フィルターのクリア

Accessing certain Dimension of Multidimensional Array

11 ビュー (過去 30 日間)
Andrew Barton
Andrew Barton 2012 年 10 月 4 日
Hi All,
Recently I've been working on a problem where I have a multidimensional array where I don't know how how many dimensions I have, but I want to get all the items that are in the ith slice (row, column, matrix, hyperplane, idk what to call it) of the jth dimension. Is there any way to do this easily in MATLAB?
As an example, for a 3x3x3 matrix, if I want the 2nd slice in the 3rd dimension, I wold go A(:,:,2), but here I don't have the luxury of typing however may semicolons I want.

採用された回答

Matt Fig
Matt Fig 2012 年 10 月 4 日
編集済み: Matt Fig 2012 年 10 月 4 日
Here is how I would do it:
% A is given, up to 8 dims here.
A = rand(max(2,randi(4,1,max(3,randi(8)))));
idx1 = 2; % Say we want the idx1th slice of
idx2 = randi(ndims(A)); % The idx2th dimension.
C = repmat({':'},1,ndims(A));
C(idx2) = {idx1}; %This is our index into A.
SLC = A(C{:}) % Here is our slice.
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 4 日
Very good answer! I like it
Matt Fig
Matt Fig 2012 年 10 月 4 日
Thanks, Azzi! The first time I saw a cell array used to index a numeric array this way was Walter showing someone how to do it 2-3 years ago. That Walter knows all the tricks!

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

その他の回答 (1 件)

Matt J
Matt J 2012 年 10 月 4 日
編集済み: Matt J 2012 年 10 月 5 日
Just because I'm fond of circumventing repmat :-)
i=2, %slice
j=3; %dim
S=size(A);
Ar=reshape(A,prod(S(1:j-1)),[], prod(S(j+1:end)));
slice = reshape( Ar(:,i,:) , [S(1:j-1), 1, S(j+1:end)]);

カテゴリ

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