Struggling with for loop if and else to set up a confusion matrix

2 Ansichten (letzte 30 Tage)
Ross Johnston
Ross Johnston am 14 Mär. 2017
Kommentiert: Jan am 14 Mär. 2017
Hi there, I have an array of 2 by 100 called Finalprobs, the first row contains results for healthy people and the second row contains results for unhealthy people. I want to set up a test to see if it is possible to differentiate between the healthy and the unhealthy patients by forming a confusion matrix.
I have set a value for K (just as an example here of 1) and if the value for unhealthy patients in the second row of Finalprobs is lower than K I want to add one to a count of TP (true positive), if the value is higher I want to add one to FN(false negative). I want to do this 100 time for all values of unhealthy patients.
For the healthy patients on the first row of Finalprobs I then want to test but here if their value is lower than K one should be added to FP(false positive), if the value is higher here I want to add one to TN(true negative). Again I want to do this 100 times for all values for healthy patients.
At the end I should therefore have 200 counts in total across the 4 parts of the confusion matrix.
Below is the code that I have tried so far by trying to piece together other bits of code from other more simple loops I have used previously, but I am a bit out of my depth here I think and have commented in what I am wanting to do but not sure if the code I have written is close to performing the job.
TP=0
TN=0
FP=0
FN=0 %set variables for the confusion matrix
K = 1 %set K to 1 for testing purposes
for confuseloopAF = 1:100 %start of for loop for unhealthy patients
if (Finalprobs(2,:)) < K %if unhealthy patients value is less
TP=TP+1; %than K add one to count of TP
else
if (Finalprobs(2,:)) > K %if unhealthy patients value is more
FN=FN+1; % than K add one to count of FN
else
confuseloopAF; %run loop again until end
end;
for confuseloopCO = 1:100 %start of loop for healthy patients
if (Finalprobs(1,:)) < K %if healthy patients value is less
FP=FP+1; %than K add one to count of FP
else
if (Finalprobs(1,:)) < K % if healthy patients value is more
TN=TN+1; % than K add one to count of TN
else
confuseloopCO; %run loop again until end
end;
You will see that I have broken it down into two separate loops as I was really struggling to get my head around putting it all into 1 loop although I know it is possible. It would probably be better in one loop but I was just trying to break it down to as simple as possible in my code to try to get it to work somehow.

Akzeptierte Antwort

Jan
Jan am 14 Mär. 2017
Bearbeitet: Jan am 14 Mär. 2017
The statement "confuseloopAF; %run loop again until end" is confusing only. Simply omit it.
Your loops miss a trailing end command. And the loop counter confuseloopAF is not used anywhere. This looks like the for loop is not clear yet. Please read: doc for.
The code does not need a loop at all:
TP = sum(Finalprobs(2,:) < K)
FN = sum(Finalprobs(2,:) > K)
FP = sum(Finalprobs(1,:) < K)
TN = sum(Finalprobs(1,:) < K) % Same as FP?!
Instead of sum the nnz can count the number of non-zero elements also.
  6 Kommentare
Ross Johnston
Ross Johnston am 14 Mär. 2017
Thankyou Jan, this has been a great help!
Jan
Jan am 14 Mär. 2017
You are welcome. This was an interesting example to compare a simple loop and the smart vectorization of Matlab. Both have advantages.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ross Johnston
Ross Johnston am 14 Mär. 2017
Managed to get it to work using the following code by myself in the end, just putting it up here incase it will help someone else searching for a similar problem...
TP=0;
TN=0;
FP=0;
FN=0;
K = 1;
for confuseloopAF = 1:100
if (Finalprobs(2,:)) < K
TP=TP+1;
else
FN=FN+1;
end
confuseloopAF;
end
for confuseloopCO = 1:100
if (Finalprobs(1,:)) < K
FP=FP+1;
else
TN=TN+1;
end
confuseloopCO;
end

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by