简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM