i have a matrix A
A = [[ 0, 0, 0, 99, 61, 0,0 ],
[ 2, 0, 0, 89, 61, 0,0 ]
[0, 0, 0, 0, 0, 89, 0 ],
[12, 0, 0, 0, 0, 0, 0 ]
[0, 0, 0, 0, 0, 0, 0 ]].
how to determine the maximum value of each row in the matrix and make other values that are not maximum to 0. for example:
A = [[ 0, 0, 0, 99, 0, 0 ,0 ],
[ 0, 0, 0, 89, 0, 0, 0 ]
[0, 0, 0, 0, 0, 89, 0 ],
[12, 0, 0, 0, 0, 0, 0 ]
[0, 0, 0, 0, 0, 0, 0 ]].
Thank you

4 件のコメント

John D'Errico
John D'Errico 2022 年 8 月 6 日
What if there are multiple values which are identical. Which one do you choose to leave non-zero? The first, the last, ALL of them?
jane
jane 2022 年 8 月 6 日
the selected value is the largest value
Torsten
Torsten 2022 年 8 月 6 日
編集済み: Torsten 2022 年 8 月 6 日
Which matrix do you want if
A = [3 5 5 ; 6 6 6 ; 7 2 9]
John D'Errico
John D'Errico 2022 年 8 月 6 日
NO. You do not understand my question. Suppose one row happens to have the elements: [0 0 0 0 100 98 100 3 0 0].
TWO of those elements are 100, the maximum. What would be the desired behavior?

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

 採用された回答

John D'Errico
John D'Errico 2022 年 8 月 6 日

0 投票

Since you did not say which one to leave non-zero, I'll asssume that all values equal to the maximum are retained.
A = randi(20,[10,5])
A = 10×5
12 15 7 1 7 11 17 15 8 14 4 1 17 19 6 11 9 3 9 9 16 20 15 14 7 12 16 11 14 12 3 19 6 17 2 17 8 7 9 2 11 6 18 11 5 8 15 12 7 20
A(A < max(A,[],2)) = 0
A = 10×5
0 15 0 0 0 0 17 0 0 0 0 0 0 19 0 11 0 0 0 0 0 20 0 0 0 0 16 0 0 0 0 19 0 0 0 17 0 0 0 0 0 0 18 0 0 0 0 0 0 20
The above code will run on R2016b or later. If two elements in a row are the same and the max in that row, it will retain them all.
If the maximum element in a row was zero, then the entire row will be zero.

1 件のコメント

jane
jane 2022 年 8 月 6 日
Thanks John

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021a

質問済み:

2022 年 8 月 6 日

コメント済み:

2022 年 8 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by