if-else statement for 4 comparison

24 Ansichten (letzte 30 Tage)
Cutie
Cutie am 12 Feb. 2022
Beantwortet: Cutie am 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

Akzeptierte Antwort

Cutie
Cutie am 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

Weitere Antworten (3)

DGM
DGM am 12 Feb. 2022
Bearbeitet: DGM am 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 am 12 Feb. 2022
Bearbeitet: David Hill am 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 Kommentare
Cutie
Cutie am 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 am 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'}

Melden Sie sich an, um zu kommentieren.


Cris LaPierre
Cris LaPierre am 12 Feb. 2022
Bearbeitet: Cris LaPierre am 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 Kommentar
Cutie
Cutie am 14 Feb. 2022
@Cris LaPierre thank you for the 'tip.' It helped me to write the code I want it. I appreciate your help!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by