Help with a loop
1 回表示 (過去 30 日間)
古いコメントを表示
Konstantina Vaitsi
2020 年 5 月 13 日
コメント済み: Konstantina Vaitsi
2020 年 5 月 15 日
I need to write a script where I have two matrixes. One S = [12 4 6 4;16 12 2 3;0 4 10 12;4 12 21 3] and one CLN = 50*ones(4). I need to create a loop where in every step all the cells in S matrix are decreased by 2 and a new CLN matrix is calculated depending on the S and the previous CLN. Specifically,
CLN(i,j) = CLN(i,j) -0.5*S(i,j) if S(i,j) <5,
CLN(i,j) = CLN(i,j)+ 0.5*S(i,j) if S(i,j) > =5
and CLN(i,j) = 0 if CLN(i,j)-0.5*S(i,j)<0.
This is what I have written so far but it doesn't work
S = [12 4 6 4;16 12 2 3;0 4 10 12;4 12 21 3];
CLN = 50*ones(4);
i = 1:4;
j = 1:4;
for t = 0:4
B = [S - 2*t];
B(B<0)=0
Y = zeros(size(B));
Y(B<5) = CLN - 0.5*B(B<5);
Y(B>=5) = CLN + 0.5*B(B>=5);
Y(CLN - 0.5*B<0) = 0(CLN - 0.5*B<0)
endfor
0 件のコメント
採用された回答
William Alberg
2020 年 5 月 15 日
S = [12 4 6 4;16 12 2 3;0 4 10 12;4 12 21 3];
CLN = 50*ones(4);
for i = 1:4
S = S - 2;
CNL_new = CLN;
% If S is below 5
index = S < 5;
CNL_new(index) = CLN(index) - 0.5*S(index);
% If S is above or equal to 5
index = S >= 5;
CNL_new(index) = CLN(index) + 0.5*S(index);
% if CLN - 0.5*S < 0
index = (CLN - 0.5*S) < 0;
CNL_new(index) = 0;
CLN = CNL_new;
end
Does this help?
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!