If else -statement doesn't work

조회 수: 9 (최근 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
Jaana
Jaana 2022년 7월 8일
Cool! I am so overwhelmed with all the coding and functions etc. in MatLab and it is so cool when someone is talented with these! I have been trying to build my own code and function to run my analyses with it, and I know there are some many parts which are made with "amateur"-mode, meaning multiple lines of code :D. But as long as it works, everything is good, right ;)? But constantly eager to learn more! Thank you very much for this simple solution!
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
Jaana
Jaana 2022년 7월 8일
Thank you for your help, this helped a lot!
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
Jaana
Jaana 2022년 7월 8일
Thank you for your help, this helped a lot, and actually the code is even more simpler than above (which I already had accepted and I don't know what is the "correct and most polite" way to handle the "Accept answer"). Very helpful and learned also few other tricks!
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by