Set zero in certain rows of an array to value x

3 ビュー (過去 30 日間)
Max Bornemann
Max Bornemann 2019 年 2 月 22 日
コメント済み: Max Bornemann 2019 年 2 月 22 日
Hi,
I have an 3x6 array A:
A=[1 1 0 1 0 1;1 1 0 0 1 1; 1 1 0 1 1 1];
x=1000000;
Now i want to set all zeros in row 2 and 3 to 1.000.000. The zeros in row 1 shall stay zero, so
A(A==0)=x;
doesn`t work, because it makes every zero in the whole array to value 1000000.
I tried it with help of an if-condition, but it doesn´t work either:
if A(2:3,:)==0
A(2:3,:)=x;
end
I will greatly appreciate any assistance.

採用された回答

Jan
Jan 2019 年 2 月 22 日
編集済み: Jan 2019 年 2 月 22 日
A = [1 1 0 1 0 1;1 1 0 0 1 1; 1 1 0 1 1 1];
x = 1000000;
% all zeros in row 2 and 3 to 1.000.000
match = (A == 0);
match(1, :) = false; % Do not access 1st row
A(match) = x
or
B = A(2:3, :);
B(B == 0) = x;
A(2:3, :) = B
  1 件のコメント
Max Bornemann
Max Bornemann 2019 年 2 月 22 日
Thank you! I´m really impressed of how fast I receive answers here, genious!

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2019 年 2 月 22 日
A((A + [0;2;2]) == 2) = 1e6;

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by