Having troubles with resizing matrices in 'for loop' and index search.

2 views (last 30 days)
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);

Accepted Answer

Walter Roberson
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
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 );
Madina Makhmutova
Madina Makhmutova on 1 Apr 2018
Dear David and Walter, thank you so much for your help! David, you are right. As I started thinking about it more, I do not need to put it back together (i'm still getting used to the "code" type of thinking:-). Here is the whole idea briefly. I have multiple traces of (r) over time, and I apply several conditions (c) at certain time points. I calculate AUC following each condition for each trace and generate a logical array, which tells me which traces changed at which condition. At the end I want to make a box plot of AUCs for the responding traces only. So I guess I just need to extract the indices from the logical array and assign them an AUC value that was calculated before. I do not need to go back to the original trace and recalculate it again. Anyway, just by typing it out, I understand it better. Thank you very much for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!