arrays - 如何在循环中从 3D 矩阵中删除全零页面?
问题描述
如何在循环中从 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
解决方案
用于any
查找具有至少一个非零值的切片的索引。使用这些索引来提取所需的结果。
idx = any(any(xy)); % idx = any(xy,[1 2]); for >=R2018b
xy = xy(:,:,idx);
推荐阅读
- c++ - C++ - 重写虚函数和多态性
- c++ - 为旧的 Linux 环境编译 C++ 应用程序
- prolog - 这个函数是尾递归的吗?为什么大列表会失败?
- cakephp-3.7 - 插件内的 CakePHP 3.7 Shell 命令无法执行
- reactjs - 对象作为 React 子对象无效(找到:带有键 {_hex} 的对象)。如果您打算渲染一组孩子,请改用数组
- sql - 有没有办法在 MariaDB ColumnStore 中实现循环连接,因为它不受支持?
- google-chrome - 如何(破解和)将 Google Doc 的绘图窗口最大化到全屏?
- r - 将 RGB 栅格拆分为多个部分,并根据条件将文件保存在 GeoTiff 中
- botframework - 如何将机器人连接到通信应用程序?
- android - 为什么 Application.getContext() 返回 null?