Find non-zero elements in a 3-D matrix

Hi,
I have a 3-D matrix containing some non-zero numerical values.I want to find the indices of these non-zero elements.
I know find() can give me row and column, but it doesn't work for a 3-D matrix. Is there another function I can use?

1 件のコメント

Mehri Mehrnia
Mehri Mehrnia 2021 年 12 月 20 日
You can use nnz(matrix), it gives you number of all non-zero elements.

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

 採用された回答

Jan
Jan 2013 年 7 月 11 日

4 投票

Why do you assume, that find() does not work for 3D arrays? Of course it does work. When used with a single output, it replies the linear index. Using this is faster than the 3 separated indices, but you can use ind2sub for a conversion:
ind = find(A);
[i1, i2, i3] = ind2sub(size(A), ind);

4 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 11 日
More simple
Jenny
Jenny 2013 年 7 月 11 日
Yes, you are right! Thanks a lot!
Shujaat Khan
Shujaat Khan 2018 年 1 月 26 日
Thanks a lot!
Nicholas Appiah
Nicholas Appiah 2021 年 9 月 16 日
How do you reshape it in the form of the original matrix

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2013 年 7 月 11 日

コメント済み:

2021 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by