フィルターのクリア

How to get the nonzero elements?

3 ビュー (過去 30 日間)
Ahmed Hassaan
Ahmed Hassaan 2012 年 3 月 10 日
hello,
I have a matrix that having a random coulmns and static number of rows ,and these columns contains alot of zeros,some of nonzero elements and i want to get the non zero elements in it and put it in a new matrix with the same static row.thanks in advance .
Ps i searched abt the nonzeros command but i cannot apply it on my code .

採用された回答

Image Analyst
Image Analyst 2012 年 3 月 10 日
Ahmed, try this code to create the cell arrays based on a row-by-row process (which looks different than if you do it column by column):
m=[...
1 2 3 4 0 6
1 0 0 4 0 6
0 0 0 0 0 0
3 4 4 0 0 6
4 5 0 2 0 7]
[rows cols] = size(m)
caRow = 1;
for r = 1 : rows
thisRow = m(r, :);
nz = thisRow(thisRow ~= 0)
if ~isempty(nz)
ca{caRow} = nz;
caRow = caRow+1;
end
end
celldisp(ca)
Results:
ca{1} =
1 2 3 4 6
ca{2} =
1 4 6
ca{3} =
3 4 4 6
ca{4} =
4 5 2 7
Note how one whole row of all zeros is not included.
  2 件のコメント
Ahmed Hassaan
Ahmed Hassaan 2012 年 3 月 10 日
thats so helpful ,but it happens when the whole row was all zeros .
Ahmed Hassaan
Ahmed Hassaan 2012 年 3 月 10 日
Thanks for ur Help Image Analyst ,its working :))

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 3 月 10 日
Are there always the same number of non-zero columns in each row? If there are not, then the matrix you are asking to output would not be rectangular. Numeric arrays must be rectangular.
If you need to have a different number of elements per row, then you need to use cell arrays.
  3 件のコメント
Ahmed Hassaan
Ahmed Hassaan 2012 年 3 月 10 日
YUP thats working thanks alot Walter :)
Image Analyst
Image Analyst 2012 年 3 月 11 日
Note the difference. If you run it on my sample matrix, Walter's has a third element of the cell array which contains a null array, whereas my example skips that row altogether. So his will have 5 elements and mine will have 4. Not sure which way you want it.

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


Image Analyst
Image Analyst 2012 年 3 月 10 日
Can you give an example? What should go in the location of the new matrix where the old matrix had zeros? That is an issue, unless you're wanting to get rid of a column if ALL of its elements are zero. For example, what would this go to:
1 2 3 4 0 6
1 0 0 4 0 6
3 4 4 0 0 6
4 5 0 2 0 7
Do you just want to get rid of column 5 so that the output matrix is 4 by 5? What about the other zeros? What would be at index (2,2), (2,3) etc. of your output matrix where you have lone, isolated zeros in your input matrix? If you need to eliminate those then you'd have to go to a cell array instead of a regular numerical matrix.
  3 件のコメント
Walter Roberson
Walter Roberson 2012 年 3 月 10 日
It is not possible to construct a numeric array that has a different number of elements in each row.
Ahmed Hassaan
Ahmed Hassaan 2012 年 3 月 10 日
hmm well its not a problem if i can put these zeros after the nonzero elements

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by