Remove dimension from high dimensional array

65 ビュー (過去 30 日間)
Francesco Porretta
Francesco Porretta 2021 年 8 月 21 日
コメント済み: Scott MacKenzie 2022 年 4 月 7 日
Hi all, I have a 8D array with size 10 8 10 8 6 7 8 9, and I want to remove one specific dimension (the one with 6 elements), obtaining a 7D array. There exist a Matlab function to do it?

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 8 月 21 日
Try this...
M = rand(10, 8, 10, 8, 6, 7, 8, 9);
whos
Name Size Bytes Class Attributes M 8-D 154828800 double
M(:,:,:,:,2:end,:,:,:) = [];
N = squeeze(M);
whos
Name Size Bytes Class Attributes M 8-D 25804800 double N 7-D 25804800 double
  4 件のコメント
Shaun Lalani
Shaun Lalani 2022 年 4 月 7 日
Hi Scott, can you please explain why you used 2:end instead of 1:end?
Scott MacKenzie
Scott MacKenzie 2022 年 4 月 7 日
That's an interesting question. Think about this for a n x m 2D matrix -- a matrix with n rows and m columns. You can't simply remove all the rows and have only the columns left. If you remove all the rows, nothing is left. You need to keep one of the rows, leaving a 1 x m "matrix"; i.e., a 1D matrix or vector.
Using 2:end in my solution is equivalent to
M(:,:,:,:,[2 3 4 5 6],:,:,:) = [];
This leaves all the data for the other dimensions that are associated with the 1st position in the 5th dimension. You could also use
M(:,:,:,:,[1 3 4 5 6],:,:,:) = [];
The resulting matrx, M, would have a different collection of data. It would have all the data from the other dimensions that are associated with the 2nd position in the 5th dimenion. Which position to choose in the 5th dimension is a matter of what the data represent and what the objectives are.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by