【发布时间】:2020-12-04 09:43:42
【问题描述】:
如何在循环中从 3D 矩阵中删除全零页面?
我想出了下面的代码,虽然它不是“完全”正确的,如果有的话。我正在使用 MATLAB 2019b。
%pseudo data
x = zeros(3,2,2);
y = ones(3,2,2);
positions = 2:4;
y(positions) = 0;
xy = cat(3,x,y); %this is a 3x2x4 array; (:,:,1) and (:,:,2) are all zeros,
% (:,:,3) is ones and zeros, and (:,:,4) is all ones
%my aim is to delete the arrays that are entirely zeros i.e. xy(:,:,1) and xy(:,:,2),
%and this is what I have come up with; it doesn't delete the arrays but instead,
%all the ones.
for ii = 1:size(xy,3)
for idx = find(xy(:,:,ii) == 0)
xy(:,:,ii) = strcmp(xy, []);
end
end
【问题讨论】:
-
这不是一个好的编辑。这个问题看起来不再一样了,似乎使发布的答案无效。它也不是minimal reproducible example。
-
抱歉,我的目的不是让任何人的努力无效,我非常感谢您的所有帮助。
-
请不要过分改变问题的含义,尤其是在发布答案之后。如果您在阅读这些答案后有新问题,请提出新问题。如果其中一个答案对您有所帮助,请考虑使用投票箭头下方的复选标记接受它,这表明您不再需要帮助。
标签: arrays matlab for-loop multidimensional-array