replacing all numbers in a certain row

14 ビュー (過去 30 日間)
David Hindahl
David Hindahl 2018 年 5 月 3 日
回答済み: David Hindahl 2018 年 5 月 3 日
Hi Guys Is there a way to replace all values in a row if just one value in the row meet a condition. Given a random 5x5 matrix. my condition is, if one number in a row is equal to -3, then all values in that row must be changed to -3.
To start with I thought that i could make a for loop, and then check each row and see if it meet the condition
Thank you in advance!
-David

採用された回答

Stephen23
Stephen23 2018 年 5 月 3 日
編集済み: Stephen23 2018 年 5 月 3 日
Where M is your matrix:
M(any(M==-3,2),:) = -3
For example:
>> M = randi(9,5);
>> M(4,2) = -3
M =
5 2 5 1 7
2 3 5 8 3
6 3 4 9 5
8 -3 9 7 2
5 6 8 8 9
>> M(any(M==-3,2),:) = -3
M =
5 2 5 1 7
2 3 5 8 3
6 3 4 9 5
-3 -3 -3 -3 -3
5 6 8 8 9

その他の回答 (1 件)

David Hindahl
David Hindahl 2018 年 5 月 3 日
Thanks man it works :-)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by