Deleting Nonzero elements in each row

Hi,
I have a 5 x 5 matrix with the 2 zeros in each row. How can I delete the nonzero elements from each row?
Any help would be appreciated. Thanks,
DB

 採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 17 日

1 投票

YourNewMatrix = zeros( size(YourMatrix,1), 2 );
After deleting all the non-zero elements of each row, only zeros are going to be left. You might as well construct an all-zero matrix directly.

1 件のコメント

Cedric
Cedric 2017 年 10 月 17 日
Ah! I had not read the question well enough ;)

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

その他の回答 (1 件)

Cedric
Cedric 2017 年 10 月 17 日
編集済み: Cedric 2017 年 10 月 17 日

0 投票

Here is one way:
>> A
A =
0 0 5 5 4
1 0 1 3 0
4 2 0 0 3
5 5 0 0 3
0 3 1 0 1
>> [~, ~, v] = find( A.' ) ;
>> B = reshape( v, 3, [] ).'
B =
5 5 4
1 1 3
4 2 3
5 5 3
3 1 1
.. and I don't know if you posted another delete question that was pretty close an hour ago or if it was someone else, but an answer was:
C = max((A == permute(B(:,1),[3,2,1])) .* permute(1:size(B,1), [1,3,2]), [], 3)
for
A = [2 3 4; 4 5 6; 7 8 9] ;
B = [7 5; 3 4; 4 3; 8 4; 9 4] ;

1 件のコメント

Cedric
Cedric 2017 年 10 月 18 日
Actually if you read again your question Dan, Walter gave you the correct answer.

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

カテゴリ

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

タグ

質問済み:

2017 年 10 月 17 日

コメント済み:

2017 年 10 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by