# Why does this loop return the same value for the variable each time?

3 vues (au cours des 30 derniers jours)
Nicholas Kavouris le 18 Mar 2022
Commenté : Image Analyst le 18 Mar 2022
Why does this loop return '4' every iteration for temp status? attached is a plot of Z, which should not return temp_status(i) as 4 each iteration as many values in z are above 15 and others below-15?
temp_status=[];
x=diff(Temp_F);
z=conv(x,ones(300,1),"same");
figure()
y=plot(z)
for i=1:length(z)
if z>=15
temp_status(i)=1;
elseif z>=-15 & z<15
temp_status(i)=2;
elseif z<-15
temp_status(i)=3;
else
temp_status(i)=4;
end
end
##### 9 commentairesAfficher 7 commentaires plus anciensMasquer 7 commentaires plus anciens
Nicholas Kavouris le 18 Mar 2022
how does indexing the z comparison work?
AndresVar le 18 Mar 2022
Modifié(e) : AndresVar le 18 Mar 2022
@Nicholas Kavouris it seems weird at first but it's a really cool feature in matlab. Once you use it a lot you will hate other languages that don't have this feature.
Here is a toy example,
temp_status = [4 4 4 4]; % for example all start as 4
z=[10 12 15 16]; % the comparable
idxs = z>=15 % the conditional indexes
idxs = 1×4 logical array
0 0 1 1
temp_status(idxs) = 1 % matlab recognizes logical indexing and only applies the assignment to the true "1" indices.
temp_status = 1×4
4 4 1 1
Only the 3rd and 4th values were changed from 4 to 1 because only indeces 3 and 4 were true (1)

Connectez-vous pour commenter.

### Réponse acceptée

Image Analyst le 18 Mar 2022
Try
temp_status=[];
x=diff(Temp_F);
z=conv(x,ones(300,1),"same");
figure()
y=plot(z)
indexes = z > 15;
temp_status(indexes) = 1;
indexes = (z >= -15) & (z <= 15);
temp_status(indexes) = 2;
indexes = z < -15;
temp_status(indexes) = 3;
##### 2 commentairesAfficher AucuneMasquer Aucune
Nicholas Kavouris le 18 Mar 2022
Modifié(e) : Nicholas Kavouris le 18 Mar 2022
this works, but I do not understand how indexing the z comparision works in terms of creating the desired cell value, could you explain? and how is this simpler than looping over all the data
Image Analyst le 18 Mar 2022
It's simpler than looping because it's faster and less lines, and vectorized. Basically the indexes is a mask that is true where the condition is true and false where the condition is false. Then the next line uses that mask as a logical vector so the assignment takes place only where the mask is true. You should learn how to do it this way. This is the real power of MATLAB. All experienced MATLAB programmers would do it that way. No serious MATLABer would use a for loop for something as simple as this. If you learn "logical indexing" and "linear indexing" and how they can be used for vectorized operations, you'll be glad you did. Invest 2 hours
You'll be glad you did because it will make you a better programmer. And you won't be using a loop all the time like those poor C programmers.

Connectez-vous pour commenter.

### Plus de réponses (2)

Simon Chan le 18 Mar 2022
x=diff(Temp_F);
z=conv(x,ones(300,1),"same");
temp_status = (z>15) + (z>=-15 & z<15)*2 + (z<-15)*3;
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Image Analyst le 18 Mar 2022
Very nice! 😊

Connectez-vous pour commenter.

Arif Hoq le 18 Mar 2022
try this:
temp_status=[];
Temp_F=A.Temp_F;
x=diff(Temp_F);
z=conv(x,ones(300,1));
figure()
y=plot(z);
for i=1:length(z)
if z(i)>=15
temp_status(i)=1;
elseif z(i)<=-15
temp_status(i)=3;
elseif z(i) > -15 & z(i) <15
temp_status(i)=2;
else
temp_status(i)=4
end
end
T=temp_status';
Z=z';
matrix=[Z(1:6) T(1:6)]
matrix = 6×2
0 2.0000 1.8000 2.0000 0 2.0000 1.8000 2.0000 0 2.0000 1.8000 2.0000
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by