简体   繁体   English

MATLAB将3D矩阵的高度向量评估为2D矩阵

[英]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. 我正在用MATLAB编程,希望使我的代码尽可能高效。 But I'm encountering a problem. 但是我遇到了一个问题。

I have a 3D matrix (row, column, heigth) And a 2D matrix (row, column) 我有一个3D矩阵(行,列,高度)和一个2D矩阵(行,列)

I would like to save the max value of the height column in the corresponding cell of the 2D matrix. 我想将height列的最大值保存在2D矩阵的相应单元格中。 This can be done with a for-loop. 这可以通过for循环来完成。

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 最好不要在Matlab中使用ij作为变量名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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