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

9 次查看(过去 30 天)
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

采纳的回答

Simon Chan
Simon Chan 2021-8-10
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 个)

Scott MacKenzie
Scott MacKenzie 2021-8-10
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by