简体   繁体   English

沿3D矩阵的每个维度获取索引

[英]Get the indices along each dimension of a 3D matrix

Is there a neater way to get the indices along each dimension of a 3D matrix? 是否有一种更整洁的方法来沿3D矩阵的每个维度获取索引? This is my solution, but I don't like its repeating and taking up three lines. 这是我的解决方案,但我不喜欢重复和占用三行内容。

rows   = 1:size(vol,1);
cols   = 1:size(vol,2);
slices = 1:size(vol,3);

you have various options, but it's not really simpler than what you have. 您有多种选择,但这并不比您拥有的要简单。

% example volumen
vol = flow(10);

% Option 1
[rows cols slices] = deal( 1:size(vol,1), 1:size(vol,2), 1:size(vol,2) )

% Option 2
indexvectors = cellfun( @(x) 1:size(vol,x), num2cell(1:3), 'uni',0 )

% Option 3
indexvectors = arrayfun( @(x) {1:size(vol,x)}, 1:3)
indexvectors = arrayfun( @(x) {1:x}, size(vol) )

The first returns three single vectors and the latter two options return a cell array with a vector for each dimension in each cell. 第一个返回三个单个向量,后两个选项返回一个单元格数组,每个单元格中每个维都有一个向量。

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

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