What's an efficient way to pick a specific slice in a high dimensional array

13 次查看(过去 30 天)
Hello all, I have an array that can be anywhere from 9 to 16 dimensional. I want to concatinate one of the dimensions into a previous dimension. For example say I have an array where size(array)=[100 100 100 5 5 5 2]. I can "get rid of" the 7th dimension of size 2 by using cat(2, array(:,:,:,:,:,:,1),array(:,:,:,:,:,:,2)). However, Using all of those colins as place holders is very cumbersome and the size of my array can vary but I always want to concatinate the 7th dimension into the second dimension of the array. Does anyone know a slick way to do this?

采纳的回答

Dave B
Dave B 2021-8-10
That's a scary looking array!
Not sure how slick it is, but if you're looking to turn those 6 colons into a number 6, you can leverage subsref. It makes more code but maybe easier to parameterize it if that's your goal?
a=rand(8, 7, 6, 5, 4, 3, 2); % using a smaller array than yours...
subsref(a,substruct('()', [repelem({':'},6) 1]));
(Just a proof of concept that it works):
b=cat(2, a(:,:,:,:,:,:,1),a(:,:,:,:,:,:,2));
c=cat(2, ...
subsref(a,substruct('()',[repelem({':'},6) 1])), ...
subsref(a,substruct('()',[repelem({':'},6) 2])));
isequal(b,c)
ans = logical
1
Something to consider: is there a form of reshape that solves this problem for you?
d = cat(3,repmat(1,4),repmat(2,4),repmat(3,4))
d =
d(:,:,1) = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 d(:,:,2) = 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 d(:,:,3) = 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
dreshape=reshape(d,4,12)
dreshape = 4×12
1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 3 3 3 3

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by