Find all non zero values in an array AND display the values

Hi,
I have a matrix of 1x1177 and I wanted to display all the nonzero values with the array and the location of them.
I am able to get the location of them using:
find(matrixOne)
But I also need the values to be displayed.
Is there a way of doing this without me having to look through the array manually?
Many thanks

 採用された回答

Thomas
Thomas 2012 年 4 月 13 日

0 投票

x = randi([0 4],1,1177)
[row,col,value]=find(x)

その他の回答 (1 件)

Wayne King
Wayne King 2012 年 4 月 13 日

0 投票

x = randi([0 4],1,1177);
indices = find(x~=0);
y = x(indices);
Or
x = randi([0 4],1,1177);
y = nonzeros(x);
The output of nonzeros is a column vector, so if you want it as a row vector, you can transpose
y = nonzeros(x)';

1 件のコメント

Jan
Jan 2012 年 4 月 13 日
Here "find(x)" is faster than "find(x~=0)".

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

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

製品

タグ

質問済み:

B M
2012 年 4 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by