フィルターのクリア

Change the values of a matrix using indices of another matrix

7 ビュー (過去 30 日間)
Anthony Siddique
Anthony Siddique 2015 年 12 月 2 日
コメント済み: Walter Roberson 2015 年 12 月 2 日
I have two matrices A and B. I want to know how do I change the values of A with using indexes of B.
A =
1 2 0 6 3
B =
1 1 0 1 0
Essentially I want to add 5 to an index of A where B==1 and keep the rest of A unchanged so I can have A be like this. I'd also prefer if someone could show me how I would do this with a while loop.
A =
6 7 0 11 3

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 12 月 2 日
編集済み: Mohammad Abouali 2015 年 12 月 2 日
mask = (B==1)
A(mask) = A(mask) + 5
if B is only zero and one then
A(logical(B)) = A(logical(B)) + 5;
and if B is already a logical variable then
A(B) = A(B) + 5;
  2 件のコメント
Anthony Siddique
Anthony Siddique 2015 年 12 月 2 日
How would I do this if it wasn't just 1's and 0's. If there were multiple conditions with other matrices?
Walter Roberson
Walter Roberson 2015 年 12 月 2 日
mask = (C == 7);
A(mask) = A(mask) + whatever_value;

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

その他の回答 (0 件)

カテゴリ

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