if-else statement for 4 comparison

27 vues (au cours des 30 derniers jours)
Cutie
Cutie le 12 Fév 2022
Réponse apportée : Cutie le 14 Fév 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

Réponse acceptée

Cutie
Cutie le 14 Fév 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

Plus de réponses (3)

DGM
DGM le 12 Fév 2022
Modifié(e) : DGM le 12 Fév 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 le 12 Fév 2022
Modifié(e) : David Hill le 12 Fév 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 commentaires
Cutie
Cutie le 12 Fév 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 le 12 Fév 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'}

Connectez-vous pour commenter.


Cris LaPierre
Cris LaPierre le 12 Fév 2022
Modifié(e) : Cris LaPierre le 13 Fév 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 commentaire
Cutie
Cutie le 14 Fév 2022
@Cris LaPierre thank you for the 'tip.' It helped me to write the code I want it. I appreciate your help!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by