Help with a loop
1 view (last 30 days)
Show older comments
Konstantina Vaitsi
on 13 May 2020
Commented: Konstantina Vaitsi
on 15 May 2020
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 Comments
Accepted Answer
William Alberg
on 15 May 2020
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?
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!