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

21 views (last 30 days)
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

Accepted Answer

Simon Chan
Simon Chan on 10 Aug 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

More Answers (1)

Scott MacKenzie
Scott MacKenzie on 10 Aug 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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!