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 件のコメント
DulceEien
DulceEien 2021 年 8 月 10 日
yes, this is working, thank yoou

サインインしてコメントする。

その他の回答 (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
  1 件のコメント
DulceEien
DulceEien 2021 年 8 月 10 日
thank you for replying

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by