Loop which Checks Values of Matrix

M = [2.7 , 1.9 , 0.75 , 0.16 , 0.35]
I have a matrix and I would like it to check each element and replace it with a new number or leave the old one depending on the parameter taken.
if element is bigger than c=1 make new element from formula
and if smaller do nothing with that number
I do not know how difficult or simple it is.
I am just starting my adventure with loops and I am counting on help here

2 件のコメント

Stephen23
Stephen23 2022 年 6 月 2 日
"...make new element from formula"
What is the formula?
Adam Rak
Adam Rak 2022 年 6 月 2 日
編集済み: Adam Rak 2022 年 6 月 2 日
a formula that uses an element from the matrix M and W
M = [2.7 , 1.9 , 0.75 , 0.16 , 0.35];
W = [a b c d e];
new_number = 2.7*a
new_number2 = 1.9*b
new_number3 = 0.75 (old number because of 0.75<c)
...

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

 採用された回答

Stephen23
Stephen23 2022 年 6 月 2 日
編集済み: Stephen23 2022 年 6 月 2 日

1 投票

Forget about loops, learn the MATLAB approach using indexing:
c = 1;
W = [4,3,5,2,3];
M = [2.7,1.9,0.75,0.16,0.35]
M = 1×5
2.7000 1.9000 0.7500 0.1600 0.3500
X = M>c;
M(X) = M(X).*W(X)
M = 1×5
10.8000 5.7000 0.7500 0.1600 0.3500

その他の回答 (1 件)

Voss
Voss 2022 年 6 月 2 日
編集済み: Voss 2022 年 6 月 2 日

1 投票

M = [2.7 , 1.9 , 0.75 , 0.16 , 0.35];
c = 1;
for ii = 1:numel(M)
if M(ii) > c
M(ii) = % new element from formula
end
end
(assuming the objective was to learn how for loops work)

カテゴリ

ヘルプ センター および File ExchangeFunctions についてさらに検索

製品

リリース

R2019b

タグ

質問済み:

2022 年 6 月 2 日

編集済み:

2022 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by