MAXIMUM MATRIKS IN MATLAB
2 次查看(过去 30 天)
显示 更早的评论
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
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
2022-8-6
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(A < max(A,[],2)) = 0
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!