Help with a loop
2 次查看(过去 30 天)
显示 更早的评论
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 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!