max value of N arrays

3 views (last 30 days)
simone zappalà
simone zappalà on 25 May 2022
Answered: Voss on 25 May 2022
I've several arrays, all are 130 rows and 1 column with different numbers created from a for cycle, so every row is the result of a for cycle i=1:130. I want to know how i can take the max value between these arrays for every cycle. At the end i need an array with 130 rows and one column each row is the max value between all the arrays.
example
x=[1,3,6,9]
y=[2,4,5,8]
max(x,y)=[2,4,6,9]

Accepted Answer

Voss
Voss on 25 May 2022
You say they're column vectors in the description, but the example uses row vectors. It doesn't really matter, you can do it either way:
x=[1,3,6,9]; % row vectors given
y=[2,4,5,8];
z=[0,5,1,2];
max([x;y;z],[],1) % row vector result
ans = 1×4
2 5 6 9
x=[1;3;6;9]; % column vectors given
y=[2;4;5;8];
z=[0;5;1;2];
max([x y z],[],2) % column vector result
ans = 4×1
2 5 6 9

More Answers (1)

MJFcoNaN
MJFcoNaN on 25 May 2022
You can concatenate all the vectors and use function of max by the given dimension. For example
x=[1,3,6,9].';
y=[2,4,5,8].';
A=[x, y]
A = 4×2
1 2 3 4 6 5 9 8
m=max(A, [], 2)
m = 4×1
2 4 6 9

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!