Having troubles with resizing matrices in 'for loop' and index search.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Madina Makhmutova
am 1 Apr. 2018
Kommentiert: Madina Makhmutova
am 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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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 Kommentare
Walter Roberson
am 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 );
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!