0

I'm programming in MATLAB and want to make my code as efficient as possible. But I'm encountering a problem.

I have a 3D matrix (row, column, heigth) And a 2D matrix (row, column)

I would like to save the max value of the height column in the corresponding cell of the 2D matrix. This can be done with a for-loop.

for i=1:row
    for j=1:column
        2D(i,j)=nanmax(3D(i,j,:));
    end
end

But is there an other way too? Something like:

2D(mask)=3D(mask,nanmax(:));

with mask being a logical matrix, possible just containing ones to make it easier.

Any help will be greatly appreciated! Thanks Jasper

jasper.l
  • 3
  • 1

1 Answers1

1

Have you tried

twoD = nanmax( threeD, [], 3 );

If you have a mask, you may use a temporal variable

tmp = nanmax( threeD, [], 3 );
twoD(mask) = tmp(mask);

PS
It is best not to use i and j as variable names in matlab

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371