Having troubles with resizing matrices in 'for loop' and index search.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2018-4-1
[r, c] = find(logical_array)
This gives vectors of row and column indices.
I cannot tell what output you are hoping for.
9 个评论
Walter Roberson
2018-4-1
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 );
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!