Having troubles with resizing matrices in 'for loop' and index search.
2 views (last 30 days)
Show older comments
Madina Makhmutova
on 1 Apr 2018
Commented: Madina Makhmutova
on 1 Apr 2018
I have a logical array of 0s an 1s. I would like to extract indices of all the cells with the value of 1, but without loosing information on columns. This gives a single column of indices.
idx = find(logical_array)
I tried for loop, but I'm not sure how to define my array before the for loop. Since the number of values varies between the columns, it generates a dimension mismatch error.
data = logical(randi(2,[10 3])-1);
idx = zeros(x,y);
for i = 1:y
r_idx(:,i) = find(data(:,i));
end
It works when I generate separate vectors and concatenate them with padcat function. But is there an easier way?
data = logical(randi(2,[10 3])-1);
idx1 = find(data(:,1));
idx2 = find(data(:,2));
idx3 = find(data(:,3));
idx = padcat(idx1, idx2, idx3);
0 Comments
Accepted Answer
Walter Roberson
on 1 Apr 2018
[r, c] = find(logical_array)
This gives vectors of row and column indices.
I cannot tell what output you are hoping for.
9 Comments
Walter Roberson
on 1 Apr 2018
I think you will find you do not need to do that when you use accumarray. But if you insist:
splits = splitapply(@(V) {V}, r, c );
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!