Help with a loop

2 visualizzazioni (ultimi 30 giorni)
Konstantina Vaitsi
Konstantina Vaitsi il 13 Mag 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

Risposta accettata

William Alberg
William Alberg il 15 Mag 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?
  1 Commento
Konstantina Vaitsi
Konstantina Vaitsi il 15 Mag 2020
I spent days on it not knowning what to do. I made a few adjustments and it finally works! Thanks a million :)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by