lundi 19 février 2018

For loop with if and elseif statements

Hi guys I have a simple problem here. I'm trying to replace values in a 1x60000 array.

Here's my code:

Where Z is the 1x60000 array.

for i = 1:length(Z)
if Z(i) == 140
   Z(i) = 1;
elseif Z(i) == 83
    Z(i) = 2;
elseif Z(i) == 52
    Z(i) = 3;
elseif Z(i) == 36
    Z(i) = 4;
elseif Z(i) == 28
    Z(i) = 5;
elseif Z(i) == 23
    Z(i) = 6;
elseif Z(i) == 125
    Z(i) = -1;
else
   Z = Z(i);
end

end

The largest value in the array is 140. However when I run the code I receive the error.

Index exceeds matrix dimensions.

Any help would be appreciated.

Thanks.

Aucun commentaire:

Enregistrer un commentaire