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!
If else -statement doesn't work
10 views (last 30 days)
Show older comments
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.
Accepted Answer
Jon
on 8 Jul 2022
Actually you can do this in just one line using MATLAB discretize
taAct.actClass = discretize(taAct.Sum,[0,30,40,60,inf]) - 1
3 Comments
Jon
on 8 Jul 2022
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.
More Answers (2)
Voss
on 8 Jul 2022
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)
Add a new column, actClass:
NewV = zeros(8,1);
taAct.actClass = NewV
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);
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;
Karim
on 8 Jul 2022
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
2 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!