How to get a matrix of all maximum values of elements of multiple matrices of the same size?

조회 수: 9 (최근 30일)
I want to get a matrix filled with elements that are the highest number of the element in other matrices on the same location in that matrix. The solution should work for large matrices, so manual labour is no option.
Simple example: I have matrix A, B and C of the same size. I want to form matrix D with in position 1,1 the max of A(1,1), B(1,1) and C(1,1). The same for position 1,2 etcetera. Matrix D is formed this way manually in the example below. I would like to have a code for this action, suitable for (very) large matrices.
if true
A= [1 2 3; 4 5 6];
B= [6 5 4; 3 2 1];
C= [4 4 4.5; 4.5 4 4];
end
%Magic happens. Please help me with a code for this sorcery to find the result:
if true
D = [6 5 4.5; 4.5 5 6]
end

채택된 답변

Jackson Burns
Jackson Burns 2019년 9월 29일
Hi Mike!
Here's a function I wrote to do the task. It assumes that the three input arrays are the same dimensions.
function outarray = findmaxes(inarray1,inarray2,inarray3)
outarray = zeros(size(inarray1));
for i = 1:numel(inarray1)
outarray(i) = max([inarray1(i) inarray2(i) inarray3(i)]);
end
end
Call it like this:
maxarray = findmaxes(A,B,C)
  댓글 수: 3
Akbar Najafi
Akbar Najafi 2021년 10월 14일
Hi Jackson
Can we find the origin position of values in the outarray? I mean, for example, outrray (i) was belonged to A and so on. In other words, I am looking for a matrix with an index; is it possible?
Thanks
Akbar

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by