Error using trace in matrices manipulation

2 views (last 30 days)
Tino
Tino on 2 May 2019
Commented: Jan on 2 May 2019
Hello I am trying to compute 2 different matrixes of various size using the code below and I am getting an error
in the line :
field2 = 'accuracy';
value2 = (2*trace(value1)+sum(sum(2*value1)))/(numOfClasses*totalSamples);
it is showing the error
Error using trace (line 12)
Matrix must be square.
Error in confusionmatStats (line 46)
field2 = 'accuracy'; value2 = (2*trace(value1)+sum(sum(2*value1)))/(numOfClasses*totalSamples);
Please your help will be appreciated
Thanks
Tino
T =
5.1000 3.5000 1.4000 0.2000
6.3453 3.2126 4.3695 1.4453
5.4421 3.3916 1.8178 0.2547
5.6874 2.8253 4.3822 1.2537
5.7000 4.3327 1.6263 0.4379
5.6042 2.9631 3.5116 1.2621
t =
5.1000 3.8000 1.9000 0.4000
6.3453 2.9379 4.1990 1.2621
4.3884 2.9958 1.2347 0.1505
5.3537 3.0000 4.3569 1.4411
using the code below:
nargin = any number ;
if nargin < 1
value1 = T;
else
value1 = confusionmat(T, t);
end
numOfClasses = size(value1,1);
totalSamples = sum(sum(value1));
field2 = 'accuracy'; value2 = (2*trace(value1)+sum(sum(2*value1)))/(numOfClasses*totalSamples);
[TP,TN,FP,FN,sensitivity,specificity,precision,f_score] = deal(zeros(numOfClasses,1));
for class = 1:numOfClasses
TP(class) = value1(class,class);
tempMat = value1;
tempMat(:,class) = []; % remove column
tempMat(class,:) = []; % remove row
TN(class) = sum(sum(tempMat));
FP(class) = sum(value1(:,class))-TP(class);
FN(class) = sum(value1(class,:))-TP(class);
end
for class = 1:numOfClasses
sensitivity(class) = TP(class) / (TP(class) + FN(class));
specificity(class) = TN(class) / (FP(class) + TN(class));
precision(class) = TP(class) / (TP(class) + FP(class));
f_score(class) = 2*TP(class)/(2*TP(class) + FP(class) + FN(class));
end
field3 = 'sensitivity'; value3 = sensitivity;
field4 = 'specificity'; value4 = specificity;
field5 = 'precision'; value5 = precision;
field6 = 'recall'; value6 = sensitivity;
field7 = 'Fscore'; value7 = f_score;
stats = struct(field1,value1,field2,value2,field3,value3,field4,value4,field5,value5,field6,value6,field7,value7);

Answers (1)

Jan
Jan on 2 May 2019
Edited: Jan on 2 May 2019
The error message is clear:
T = [ ...
5.1000 3.5000 1.4000 0.2000
6.3453 3.2126 4.3695 1.4453
5.4421 3.3916 1.8178 0.2547
5.6874 2.8253 4.3822 1.2537
5.7000 4.3327 1.6263 0.4379
5.6042 2.9631 3.5116 1.2621];
t = [ ...
5.1000 3.8000 1.9000 0.4000
6.3453 2.9379 4.1990 1.2621
4.3884 2.9958 1.2347 0.1505
5.3537 3.0000 4.3569 1.4411];
size(T)
size(confusionmat(T, t))
trace requires a square matrix as input. So which sizes do you get?
Please note, that it is unlikely that readers can guess broken code without any further details. Seeing the failing code only does not allow to guess its intention.
  3 Comments
Jan
Jan on 2 May 2019
@Tino: You provide a non-square matrix to the trace command. This cannot work, because a trace is defined for a square matrix only. The error message tells this clearly already. We cannot guess, what you want to do instead, because the current code is not meaningul in a mathematical sense.
"actually I know there are of different matrices what other way do I mingle around that" - I do not understand, what this means and cannot know, which formula you want to implement.

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!