Argument of max values of multiple 2D arrays

2 views (last 30 days)
H
H on 16 Apr 2022
Answered: p on 16 Apr 2022
Hello,
I have several 2D arrays (let's say I have 3, with 5x10 dimension for each), each array corresponds to one argument:
A1 corresponds to the argument 10, A2 to the argument 20 and A3 to the argument 30.
What i would be interested in, would be to have an array in which I would have for each element the argument corresponding to the maximum of the values of the 3 arrays.
That's not very clear so I'll give an example, if I had two 2x2 arrays:
A1 =
1000 25
3 258
A2 =
1 30
247 259
The array that I want would look like that:
C =
10 20
20 20
I can't really figure out how to do it, is it possible to do it without a loop? (I got lots of arrays with big dimensions so that wouldn't be super convenient)
Thanks for your help!

Accepted Answer

p
p on 16 Apr 2022
This seems to do what you want.
clc; clear all; close all;
A1=[1,2,3 ; 4,5,6 ; 7,8,9];
A2=[2,2,3 ; 3,4,5 ; 5,6,7];
A3=[0,1,2 ; 3,4,5 ; 9,9,6];
arg=[10,20,30];
A=cat(3,A1,A2,A3);
[~,M]=max(A,[],3);
M(:)=arg(M(:));
M

More Answers (1)

Simon Chan
Simon Chan on 16 Apr 2022
A1 = [1000 25;3 258];
A2 = [1 30;247 259];
C = 10*(A1>A2) + 20*(A2>A1)
C = 2×2
10 20 20 20

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by