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

조회 수: 20 (최근 30일)
DulceEien
DulceEien 2021년 8월 10일
댓글: DulceEien 2021년 8월 10일
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