jasper.l
jasper.l

Reputation: 3

MATLAB evaluate height vector of a 3D matrix into a 2D matrix

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

Upvotes: 0

Views: 235

Answers (1)

Shai
Shai

Reputation: 114816

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

Upvotes: 1

Related Questions