Argument of max values of multiple 2D arrays

2 次查看(过去 30 天)
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!

采纳的回答

Stephane
Stephane 2022-4-16
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

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by