how can a make a loop for a double value in Matlab?

17 vues (au cours des 30 derniers jours)
DulceEien
DulceEien le 10 Août 2021
Commenté : DulceEien le 10 Août 2021
when I obtain the data for the array I'm having the ID in a 22x1 double data, and when I run the if condition I have a problem with the size
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
for i = 1:length(T1.ID)
if ID(i) == 0
EI(i) = 'Very high';
elseif ID(i) == 1
EI(i) = 'High';
elseif ID(i) == 2
EI(i) = 'Medium';
elseif ID(i) == 3
EI(i) = 'Low';
end
end
the other values are taking a colum of the table generated. Sorry, I'm kind of new with matlab, thanks in advance

Réponse acceptée

Simon Chan
Simon Chan le 10 Août 2021
I think it should put into a cell array:
EI = cell(1,length(T1.ID)); % Cell array
for i = 1:length(T1.ID)
if ID(i) == 0
EI{i} = 'Very high';
elseif ID(i) == 1
EI{i} = 'High';
elseif ID(i) == 2
EI{i} = 'Medium';
elseif ID(i) == 3
EI{i} = 'Low';
end
end
  1 commentaire
DulceEien
DulceEien le 10 Août 2021
yes, this is working, thank yoou

Connectez-vous pour commenter.

Plus de réponses (1)

Scott MacKenzie
Scott MacKenzie le 10 Août 2021
It's not clear what 19 variables you have in the table T1, but I suspect your error will disappear via...
for i = 1:length(ID) % change from T1.ID to just ID
if ID(i) == 0
EI(i) = 'Very high';
elseif ID(i) == 1
EI(i) = 'High';
elseif ID(i) == 2
EI(i) = 'Medium';
elseif ID(i) == 3
EI(i) = 'Low';
end
end

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by