Clear Filters
Clear Filters

if-else statement for 4 comparison

16 views (last 30 days)
Cutie
Cutie on 12 Feb 2022
Answered: Cutie on 14 Feb 2022
I want to use if-else & for-loop to make comparison where outputs can be one of four options- TP,TN,FP,FN
Supposed I have two row vectors of whose elements are 1s & 0s.
A= [0;1;0;1;1;0;1];
B= [0;1;1;0;1;1;0];
for i=1:7
if A(i) ==1 && B(i)==1
output TP=1
elseif A(i) ==1 && B(i)==0
output FN=1
elseif A(i) ==0 && B(i)==0
output TN=1
else
output FP=1
end
end
Unrecognized function or variable 'output'.
I need help on how to properly code this. My idea above is not working

Accepted Answer

Cutie
Cutie on 14 Feb 2022
Thank you @Cris LaPierre @DGM @David Hill for your help. I have been able to navigate it
A= [0;1;0;1;1;0;1;0;0;1];
B= [0;1;1;0;1;1;0;1;0;1];
TP=zeros(1,size(A,1));
TN=zeros(1,size(A,1));
FP=zeros(1,size(A,1));
FN=zeros(1,size(A,1));
for i=1:7
if A(i) ==1 && B(i)==1
TP(i)=1;
elseif A(i) ==1 && B(i)==0
FN(i)=1;
elseif A(i) ==0 && B(i)==0
TN(i)=1;
else
FP(i)=1;
end
end

More Answers (3)

DGM
DGM on 12 Feb 2022
Edited: DGM on 12 Feb 2022
You can do it something like this, assuming your inputs are binarized as described.
A = [0;1;0;1;1;0;1];
B = [0;1;1;0;1;1;0];
strmap = {'TN';'FN';'FP';'TP'};
output = strmap(A + 2*B + 1)
output = 7×1 cell array
{'TN'} {'TP'} {'FP'} {'FN'} {'TP'} {'FP'} {'FN'}

David Hill
David Hill on 12 Feb 2022
Edited: David Hill on 12 Feb 2022
A= [0;1;0;1;1;0;1];
B= [0;1;1;0;1;1;0];
output=cell(size(A));
output(A==1&B==1)={'TP'};
output(A==1&B==0)={'FN'};
output(A==0&B==0)={'TN'};
output(A==0&B==1)={'FP'};
  2 Comments
Cutie
Cutie on 12 Feb 2022
@David Hill, thank you for offering your help. However, the vectors can be as much as 50000 rows. So I want it programmed.
DGM
DGM on 12 Feb 2022
It is programmed. David's method uses logical indexing to create one cell array of character vectors which is the same size as A.
A= [0;1;0;1;1;0;1];
B= [0;1;1;0;1;1;0];
output=cell(size(A));
output(A==1&B==1)={'TP'};
output(A==1&B==0)={'FN'};
output(A==0&B==0)={'TN'};
output(A==0&B==1)={'FP'};
output % show the result
output = 7×1 cell array
{'TN'} {'TP'} {'FP'} {'FN'} {'TP'} {'FP'} {'FN'}

Sign in to comment.


Cris LaPierre
Cris LaPierre on 12 Feb 2022
Edited: Cris LaPierre on 13 Feb 2022
I modified your code to run it here and display the error.
You are close, but your syntax is incorrect. Assignment in MATLAB is done with the '='. For example
output = 'TN'
Also, each loop will overwrite the previous ouput value with the new one. Once finished, you will only have the result of the final loop. Use your loop index to assign the current output to a specific location in the vector output. For example
output(i) = 'TN'
  1 Comment
Cutie
Cutie on 14 Feb 2022
@Cris LaPierre thank you for the 'tip.' It helped me to write the code I want it. I appreciate your help!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!