how to scan a matrix row by row and save index of elements

3 ビュー (過去 30 日間)
ali eskandari
ali eskandari 2021 年 9 月 2 日
編集済み: Matt J 2021 年 9 月 2 日
A =
1 2 3 4
0.5 5 6 2
5 3 1 3
I have a matrix-like A, and my aim is to scan the matrix row by row and save the index of values that is greater than 2 (or any threshold). In fact, I want to have an array like this:
H = {[3,4];[2,3];[1,2,4]}
I tried to do it like below but I don't know how I can append an elemnt to the end of array H.
If you can suggest anything faster way, I would appreciate it.
for i = 1:width(A)
H{i} = [];
for j = 1:height(A)
if A(i,j) > 2
H{i} = % how to indicate the index of the matxis that is true for the if condition
end
end
end
  1 件のコメント
Matt J
Matt J 2021 年 9 月 2 日
編集済み: Matt J 2021 年 9 月 2 日
I wonder why you wouldn't simply use a sparse binarymatrix:
H=sparse(A>2);

サインインしてコメントする。

採用された回答

Matt J
Matt J 2021 年 9 月 2 日
編集済み: Matt J 2021 年 9 月 2 日
A = [1 2 3 4
0.5 5 6 2
5 3 1 3] ;
[I,J]=find(A>2);
H=accumarray(I,J,[],@(x){x.'});
H{:}
ans = 1×2
3 4
ans = 1×2
2 3
ans = 1×3
1 2 4
  2 件のコメント
ali eskandari
ali eskandari 2021 年 9 月 2 日
Thank you @Matt J but this will return the values not the index of each.
Matt J
Matt J 2021 年 9 月 2 日
編集済み: Matt J 2021 年 9 月 2 日
But I demonstrated it for you on your example!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by