How to efficiently compare two matrix to get a single reference value?
Mostrar comentarios más antiguos
May I know how make the following code more efficient and compact. Specifically, I want to reduce the line
ConfMat (logical (((TrueVal==xx) .*(Predicted==xx))))=xx;
Here are the complete code and its output
TrueVal= [1 1 1 2 2 2 3 3 3 1 2]';
Predicted=[1 2 3 1 2 3 1 2 3 3 2]';
ConfMat = single(ones(length(TrueVal), 1));
ConfMat (logical (((TrueVal==1) .*(Predicted==2))))=4;
ConfMat (logical (((TrueVal==1) .*(Predicted==3))))=7;
ConfMat (logical (((TrueVal==2) .*(Predicted==1))))=2;
ConfMat (logical (((TrueVal==2) .*(Predicted==2))))=5;
ConfMat (logical (((TrueVal==2) .*(Predicted==3))))=8;
ConfMat (logical (((TrueVal==3) .*(Predicted==1))))=3;
ConfMat (logical (((TrueVal==3) .*(Predicted==2))))=6;
ConfMat (logical (((TrueVal==3) .*(Predicted==3))))=9;
% Final output
% ConfMat= [1;4;7;2;5;8;3;6;9;7;5]
Thanks in advance
3 comentarios
Rik
el 15 de En. de 2019
Instead of changing that line of code, why don't you use a loop to process the vectors?
madhan ravi
el 15 de En. de 2019
Second Rik , I can't think of any solution other than that.
balandong
el 15 de En. de 2019
Respuesta aceptada
Más respuestas (1)
Bruno Luong
el 15 de En. de 2019
TrueVal= [1 1 1 2 2 2 3 3 3 1 2]';
Predicted=[1 2 3 1 2 3 1 2 3 3 2]';
[ut,~,it] = unique(TrueVal);
[up,~,ip] = unique(Predicted);
ConfM = [1 4 7;
2 5 8;
3 6 9];
assert(size(ConfM,1)==length(ut),'ConfM must have same #rows than #TrueVal');
assert(size(ConfM,2)==length(up),'ConfM must have same #rows than #Predicted');
ConfMat = ConfM(sub2ind(size(ConfM),it,ip))
returns
ConfMat =
1
4
7
2
5
8
3
6
9
7
5
1 comentario
balandong
el 17 de En. de 2019
Categorías
Más información sobre MATLAB Support Package for Parrot Drones en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!