Tony YEe
Tony YEe

Reputation: 235

Find the maximum value from the each set

I have 3 sets of data as shown in below:

A=[3 1 4 2;7 9 8 3;7 5 3 6;4 1 9 3]

B=[1 0 4 5;7 7 1 3;4 7 6 5;2 2 1 9] 

C=[9 1 3 7;9 6 5 5;1 4 3 2;0 3 2 1]

I need to find out the maximum value when comparing with each other. for instance, for matrix [1x1] from each set, A=3,B=1,C=9,thus maximum number is 9

for matrix [1x2],maximum value=1 and so on..

so the

final result =[9 1 4 7;9 9 8 5;7 7 6 6;4 3 9 9]

Any suggestion to solve this problem? Thanks!

Upvotes: 0

Views: 129

Answers (2)

fpe
fpe

Reputation: 2750

You could do like this:

A=[3 1 4 2;7 9 8 3;7 5 3 6;4 1 9 3];
 A = reshape(A,[1,numel(A)]);
 B=[1 0 4 5;7 7 1 3;4 7 6 5;2 2 1 9] ;
 B = reshape(B,[1,numel(B)]);
 C=[9 1 3 7;9 6 5 5;1 4 3 2;0 3 2 1];
 C = reshape(C,[1,numel(C)]);
 D = [A;B;C];
 for ii = 1:size(D,2)
  res(1,ii) = max(D(:,ii));
 end
 res = reshape(res,[4,4]);

Upvotes: 0

H.Muster
H.Muster

Reputation: 9317

You can use max. For the case with 3 matrices, just use

max(A, max(B, C))

If you have more than three matrices, writing those max statements can get tiring, so you would use cat before taking the maximum

max(cat(3, A, B, C, D, E), [], 3)

Upvotes: 5

Related Questions