フィルターのクリア

Indexing one particular dimension regardless of number of dimensions

1 回表示 (過去 30 日間)
Jacob Matthews
Jacob Matthews 2015 年 2 月 23 日
コメント済み: Guillaume 2015 年 2 月 23 日
I have a structure of several dimension (7 right now) that I will be adding dimensions to in the future. In a section of my code I need to isolate the third dimension into it's three components which right now I do like
outX = in(:,:,1,:,:,:,:); outY = in(:,:,2,:,:,:,:); outZ = in(:,:,3,:,:,:,:);
Is there a way to generalize this without having to edit the number of colons in the statement?
ex:
outX = coolFunction(in,3,1); outY = coolFunction(in,3,2); outZ = coolFunction(in,3,3);
Thanks!

採用された回答

Guillaume
Guillaume 2015 年 2 月 23 日
function out = coolFunction(in, dim, page)
validateattributes(dim, {'numeric'}, {'scalar', 'positive', '<=', ndims(in)});
validateattributes(page, {'numeric'}, {'scalar', 'positive', '<=', size(in, dim)});
alldims = arrayfun(@(d) 1:d, size(in), 'UniformOutput', false);
alldims{dim} = page;
out = in(alldims{:});
end
  2 件のコメント
Jacob Matthews
Jacob Matthews 2015 年 2 月 23 日
Super cool answer... and I won't pretend to understand it initially, but I'll study it.
I'm mostly glad I wasn't overlooking some existing function or indexing notation.
Thanks!
Guillaume
Guillaume 2015 年 2 月 23 日
Basically, it creates a cell array (with arrayfun) where each cell is an array from 1 to the size of each dimension (what colon would do), then replace the cell of the required dimension with just the page number you want in that dimension.
The last line uses expansion of cell array into comma separated list to index the matrix.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by