Clear Filters
Clear Filters

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

7 views (last 30 days)
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?

Accepted Answer

Dave B
Dave B on 10 Aug 2021
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

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!