Help with a loop
조회 수: 1 (최근 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에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!