Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare multiple matrices matlab

I have multiple matrices of the same size and want to compare them. As a result I need a matrix which gives me the biggest of the 3 for every value.

I will clarify what i mean with an example:

I have 3 matrices with data of 3 persons.

I would like to compare these 3 and get a matrix as result.

In that matrix every cell/value should be the name of the matrix who had the highest value for that cell. So if in the 3 matrices the first value (1 colum, 1 row) is accordingly 2, 5, 8 the first value of the result matrix should be 3 (or the name of the 3 matrix).

like image 568
user2661122 Avatar asked Aug 07 '13 14:08

user2661122


2 Answers

If the three matrices are A, B, C, do this:

[~, M] = max(cat(3,A,B,C),[],3);

It creates a 3D "matrix" and maximizes across the third dimension.

like image 87
Luis Mendo Avatar answered Oct 03 '22 10:10

Luis Mendo


Concatenate them on the 3rd dimension, and the use the SECOND output from max to get exactly what you want

A = rand(3,3);
B = rand(3,3);
C = rand(3,3);

D = cat(3, A, B, C)

[~, Solution] = max(D, [], 3)

e.g.:

D =

ans(:,:,1) =

   0.70101   0.31706   0.83874
   0.89421   0.33783   0.55681
   0.68520   0.11697   0.45631

ans(:,:,2) =

   0.268715   0.213200   0.124450
   0.869847   0.999649   0.153353
   0.345447   0.023523   0.338099

ans(:,:,3) =

   0.216665   0.297900   0.604734
   0.103340   0.767206   0.660668
   0.127052   0.430861   0.021584

Solution =

   1   1   1
   1   2   3
   1   3   1
like image 44
Dan Avatar answered Oct 03 '22 09:10

Dan