# Finding the Maximum of 6 matrices

3 views (last 30 days)
serena solanki on 24 Apr 2018
Answered: Siyu Guo on 29 Apr 2018
I have 6 41*41 matrices. They contain peak displacement data for a combination of variables which I have changed
I.e. I would have a matrix which would look like this for example: where the kiso values are going down and the ciso vaules are going across and the values are the correspsonding data for each combination.
M1=
ciso
kiso 1 2 3
1 3.4 3 0.5
2 8 5.6 0.23
3 2 4.3 0.56
M2=
ciso
kiso 1 2 3
1 4 5 6
2 0.1 5.2 0.2
3 9 4 0.5
Max=
ciso
kiso 1 2 3
1 4 5 6
2 8 5.6 0.23
3 9 4.3 0.56
How do I compare each matrix and build a separate matrix which has the maximum value for each kiso and ciso combination as I have done in the example?
I have tried the matrix max comparison max(A,B) etc but I get an error saying there are too many input arguments as I am comparing 6 matrices not 2. max_building=max(Peak_mat1,Peak_mat2,Peak_mat3,Peak_mat4,Peak_mat5,Peak_mat6);

njj1 on 24 Apr 2018
You can use max(X,[],dim) to specify the dimension from which to compute the maximum value. For example, if you let dim=2, then this computes the maximum value across each row of your matrix, resulting in a column vector; while dim=1 takes the maximum over each column of your matrix, resulting in a row vector. You could also try to re-arrange your data into a 41x41x6 array, where each "slice" in this 3-D cube is one of your original matrices. If you then entered max(X,[],3), you would get a 41x41 matrix where each entry is the maximum value of that particular (i,j) location for each of the 6 41x41 matrices. You could then pare this down into a maximum value for all columns and all rows over all the 6 matrices. One way to do this is: max(max(X,[],3),[],2) to get the max across each row for all 6 arrays. To do this same thing for columns: max(max(X,[],3),[],1). Hope this helps!
serena solanki on 29 Apr 2018
Thank you very much for this!
I will try and combine the matrices into a 41*41*6 array. I am a little unsure how to save the data into an array as I want to make sure its in the right order.
Thanks,
Serena

dpb on 24 Apr 2018
The problem is the creation of sequentially named variables instead of using a form of data storage that makes addressing the data easy programmatically.
The simplest conceptually for the 2D arrays would be to create a 3D array where each of the six is a plane (3rd dimension); then you can simply address each by the desired index 1 thru 6.
If you create the M array in that fashion for the above case you'd have
M(:,:,1) =
3.4000 3.0000 0.5000
8.0000 5.6000 0.2300
2.0000 4.3000 0.5600
M(:,:,2) =
4.0000 5.0000 6.0000
0.1000 5.2000 0.2000
9.0000 4.0000 0.5000
>>
and the desired max are simply
>> max(M,[],3)
ans =
4.0000 5.0000 6.0000
8.0000 5.6000 0.2300
9.0000 4.3000 0.5600
>>
serena solanki on 29 Apr 2018
So as long as I call each matrix M(:,:1)----M(:,:,6) I can use the max(M,[],3) ?? or would I have to use 41 i.e. max(M,[],41) as I have 41 columns ??
I just want to make sure I have understood what you have said.
Thanks,
Serena

Siyu Guo on 29 Apr 2018
Guess max(cat(3,M1,M2,M3,M4,M5,M6), [], 3) should do.