フィルターのクリア

delete rows of a matrix

4 ビュー (過去 30 日間)
dav
dav 2013 年 3 月 18 日
I have a vector
A = [ 1 0 2;
0 3 4;
3 5 0;
0 0 0;
1 1 1 ]
I need to delete the rows with ALL ELEMeTNS = 0 that means I want to get
B = [ 1 0 2;
0 3 4;
3 5 0;
1 1 1 ] form A.
Can someone please help me with this? I have to use this command inside a LOOP. So, I don't know in which row this could happen.
So, I need a code that finds and deletes the rows with all elements equal to zero.
Thanks.

採用された回答

Walter Roberson
Walter Roberson 2013 年 3 月 18 日
~any(A(J,:)) when you are examining row J.
  1 件のコメント
dav
dav 2013 年 3 月 18 日
Thank you very much!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 3 月 18 日
Once you graduate to the more MATLAB-ish, non-loop, vectorized way, you'd do it like this:
clc;
A = [ 1 0 2;
0 3 4;
3 5 0;
0 0 0;
1 1 1 ]
rowsToKeep = any(A,2)
B = A(rowsToKeep, :)
Why do you have to do it in a loop? Seems like a dumb requirement.
  2 件のコメント
dav
dav 2013 年 3 月 18 日
I have to employ this in a simulation program. thanks.
Image Analyst
Image Analyst 2013 年 3 月 19 日
You can employ my code in a simulation program. Being in a simulation program is not a reason why it has to be a for loop. But whatever...it doesn't matter....

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by