How to index the matrix only the first dimension of the rest
22 ビュー (過去 30 日間)
古いコメントを表示
For example, I have an matrix A=zeros(5,4,3)
And B=A(1,:,:)
Is there any way that I don't have to use the latter two ':'?
Something simple like B=A(1,?) that takes whatever from the second dimension onwards?
0 件のコメント
回答 (4 件)
Bruno Luong
2022 年 8 月 31 日
A = rand(2,3,4,5);
C=repmat({':'},1,ndims(A)-1);
% C=repmat({:},1,100); if you don't want to be depend on A
B = A(1,C{:});
0 件のコメント
Stephen23
2022 年 9 月 1 日
This will be quite efficient because none of the data in memory is moved around, only the array header is accessed.
It also works for non-scalar X.
B = A(X,:);
S = size(A);
S(1) = size(B,1);
B = reshape(B,S);
Walter Roberson
2022 年 8 月 31 日
B=A(1,:);
However this will treat it like
temp = reshape(A, size(A, 1), []);
B = temp(1,:);
which would give you a 1 x 12 not a 1 x 4 x 3
2 件のコメント
Bruno Luong
2022 年 9 月 1 日
@Xuze Liu Walter explains the RHS of
B=A(1,:)
is like reshaping A with all the traling dimension grouped. You don't have to.
Cris LaPierre
2022 年 8 月 31 日
It depends what you want the ouput dimensions to be. If you just want them in a vector, then you can just use a single colon.
A=rand(5,4,3)
A(1,:)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!