Compare multiple matlab matrices

I have several matrices of the same size and want to compare them. As a result, I need a matrix that gives me the largest of 3 for each value.

I will explain what I mean with an example:

I have 3 matrices with data from 3 people.

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

In this matrix, each cell / value should be the name of the matrix that had the highest value for this cell. Thus, if in 3 matrices the first value (1 column, 1 row) corresponds to 2, 5, 8 , the first value of the result matrix should be 3 (or matrix name 3).

+6
source share
3 answers

If the three matrices A, B, C, do the following:

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

It creates a 3D matrix and maximizes it in a third size.

+5
source

Combine them in the 3rd dimension and 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) 

eg:.

 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 
+2
source

edit Since I did not know about the second argument of the max function, here is what you should NOT use:

old Well, quick and dirty:

 x=[2 5 8]; w=max(x) [~,loc] = ismember(w,x) 
0
source

Source: https://habr.com/ru/post/951224/


All Articles