If else calculation problem in table

1 回表示 (過去 30 日間)
Kasih Ditaningtyas Sari Pratiwi
Kasih Ditaningtyas Sari Pratiwi 2017 年 10 月 22 日
コメント済み: Image Analyst 2017 年 10 月 25 日
Hi! I have a problem in using if else for tables in matlab. It does not result in error, but it does not show the real result, here is my code :
for x=1:size(finalCSVnew{:,2},2)
if finalCSVnew{:,2}(x) > 0;
finalCSVnew{:,13}(x)=finalCSVnew{:,7}(x);
else
finalCSVnew{:,13}(x)=0;
end
end
and I attach my source data here in the picture. If in column 2 the value is > 0, the value in Var13 should be the same as in column 7. However it remains 0. Please help me to find whats wrong with my code. Actually I am also not sure about how to define x, because I see the example in another case. Maybe I also got wrong from that. Thank you very much for your help.

採用された回答

Peter Perkins
Peter Perkins 2017 年 10 月 22 日
編集済み: Peter Perkins 2017 年 10 月 22 日
This is almost certainly better done without a loop, but it's hard to tell what you are doing. Something like
i = finalCSVnew.Var2 > 0;
finalCSVnew.Var13(i) = finalCSVnew.Var7(i);
finalCSVnew.Var13(~i) = 0;
In any case, your bug would seem to be that you are looping over columns of a column vector.
  2 件のコメント
Kasih Ditaningtyas Sari Pratiwi
Kasih Ditaningtyas Sari Pratiwi 2017 年 10 月 22 日
Thanks. But I got an error: You cannot subscript a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts). Use a row subscript and a variable subscript. I actually have not defined yet Var13 so I use :
i = finalCSVnew{:,2} > 0;
finalCSVnew{:,13}(i) = finalCSVnew{:,7}(i);
finalCSVnew{:,13}(~i) = 0;
and I got an error as the result.
Image Analyst
Image Analyst 2017 年 10 月 25 日
Can you attach your data in a .mat file?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by