If else -statement doesn't work

2 ビュー (過去 30 日間)
Jaana
Jaana 2022 年 7 月 8 日
コメント済み: Jon 2022 年 7 月 8 日
I am trying to create a new column to a table with a value depending on the result on previous column. I am a beginner with MatLab (and coding) and have tried to fix the problem (using documentation, answers, YouTube and Google), but can't just figure where the problem is, which is probably something very simple (and me making a amateur mistake).
Here is the code, first two lines creating a table and summarizing the columns.
taAct= array2table(randi([0 30],8,3)); %making a table as my final dataset is as table
taAct.Sum = sum(taAct.Variables,2)
if taAct.Sum >= 60
taAct.actClass = 3;
elseif taAct.Sum >= 40
taAct.actClass = 2;
elseif taAct.Sum >= 30
taAct.actClass = 1;
else
taAct.actClass = 0
end
The problem is that it gives an error: "To assign to or create a variable in a table, the number of rows must match the height of the table".
I also tried to add the column first only with zeros, before the if-statement:
NewV = zeros(8,1)
taAct.actClass = NewV
But that didn't help either, still the same error.
I even tried to execute this with an array, but still the last column stays empty. So, please be kind and point me out, where the mistake is. I know how to do this with indeces, but with multiple steps, so I would love to learn how to do this more efficiently and correctly.
  1 件のコメント
Jaana
Jaana 2022 年 7 月 8 日
Ps. I wan't to thank for all the solutions and I now know multiple ways to execute this. But I still don't know what was wrong with the original if-statement and why it did not work. I would be very eager to learn from my mistake to avoid similar ones in the future. So if someone would be up to shortly explain the reason, I would be very grateful!

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

採用された回答

Jon
Jon 2022 年 7 月 8 日
Actually you can do this in just one line using MATLAB discretize
taAct.actClass = discretize(taAct.Sum,[0,30,40,60,inf]) - 1
  3 件のコメント
Jon
Jon 2022 年 7 月 8 日
Your welcome. For this particular situation it is nice that there is some built in functionality. The approaches given in other answers, showing you how to use the logical indexing, are very instructive though and really worth understanding as they can be applied for many more general situations.

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

その他の回答 (2 件)

Voss
Voss 2022 年 7 月 8 日
Initialize the table:
taAct= array2table(randi([0 30],8,3)); %making a table as my final dataset is as table
taAct.Sum = sum(taAct.Variables,2)
taAct = 8×4 table
Var1 Var2 Var3 Sum ____ ____ ____ ___ 12 16 17 45 25 5 30 60 7 19 30 56 29 25 21 75 2 17 5 24 0 19 27 46 15 17 29 61 5 27 5 37
Add a new column, actClass:
NewV = zeros(8,1);
taAct.actClass = NewV
taAct = 8×5 table
Var1 Var2 Var3 Sum actClass ____ ____ ____ ___ ________ 12 16 17 45 0 25 5 30 60 0 7 19 30 56 0 29 25 21 75 0 2 17 5 24 0 0 19 27 46 0 15 17 29 61 0 5 27 5 37 0
Use logical indexing to set the value of actClass in more than one row at a time:
idx = taAct.Sum >= 60;
taAct.actClass(idx) = 3;
idx = taAct.Sum < 60 & taAct.Sum >= 40;
taAct.actClass(idx) = 2;
idx = taAct.Sum < 40 & taAct.Sum >= 30;
taAct.actClass(idx) = 1;
% idx = taAct.Sum < 30; % this one's not necessary since
% taAct.actClass(idx) = 0; % actClass is 0 by default
disp(taAct);
Var1 Var2 Var3 Sum actClass ____ ____ ____ ___ ________ 12 16 17 45 2 25 5 30 60 3 7 19 30 56 2 29 25 21 75 3 2 17 5 24 0 0 19 27 46 2 15 17 29 61 3 5 27 5 37 1
A more compact way to write the same commands:
taAct.actClass(taAct.Sum >= 60) = 3;
taAct.actClass(taAct.Sum < 60 & taAct.Sum >= 40) = 2;
taAct.actClass(taAct.Sum < 40 & taAct.Sum >= 30) = 1;
  2 件のコメント
Voss
Voss 2022 年 7 月 8 日
You're welcome!

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


Karim
Karim 2022 年 7 月 8 日
The easiest method will be to first create a zero array, and then fill in the values according to the required logic:
% intialize
taAct= array2table(randi([0 30],8,3));
taAct.Sum = sum(taAct.Variables,2);
taAct.actClass = zeros(size(taAct.Sum));
% first assign the values where the sum is larger then 30
taAct.actClass( taAct.Sum >= 30 ) = 1;
% now assign the values for 40
taAct.actClass( taAct.Sum >= 40 ) = 2;
% finally for the values larger then 60
taAct.actClass( taAct.Sum >= 60 ) = 3;
% show resulting table
taAct
taAct = 8×5 table
Var1 Var2 Var3 Sum actClass ____ ____ ____ ___ ________ 2 30 2 34 1 27 23 24 74 3 26 13 1 40 2 4 5 8 17 0 0 2 18 20 0 17 8 30 55 2 10 22 30 62 3 26 6 4 36 1
  2 件のコメント
Jon
Jon 2022 年 7 月 8 日
編集済み: Jon 2022 年 7 月 8 日
You can unaccept an answer, and then select as reason, found better answer. I don't think it is impolite, and is useful as it help sort the answers for others who might have the same problem.

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by