How to make a new column in my table
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have the following in my table (Results) of 550 rows:
A = audsActual %in the second row
P = audsPreds %in the third row
I want to make a 4th column that checks if for each row, if A = P, then it says 1. if not, it says 0.
I wrote this code but it is not working:
if Results(2,:) == Results(3,:)
Results (4,:) = 1
elseif Results (4,:) = 0
It's giving me an error message. Please help, thank you!
0 个评论
回答(1 个)
Walter Roberson
2023-1-25
elseif requires a expression after it, but you coded an assignment.
You just want else not elseif
Remember that
if Results(2,:) == Results(3,:)
is going to test all of row 2 against all of row 3, creating a logical vector of true and false. if and while consider a test to be true only if all of the values being tested are non-zero, so the code you wrote is equivalent to
if all(Results(2,:) == Results(3,:))
which is not what you want.
What you really need to do is read about logical indexing. https://blogs.mathworks.com/loren/2013/02/20/logical-indexing-multiple-conditions/
1 个评论
Walter Roberson
2023-1-25
Though in this particular case you could just code
Results(4,:) = Results(2,:) == Results(3,:);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!