What's an efficient way to pick a specific slice in a high dimensional array
13 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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)
d = cat(3,repmat(1,4),repmat(2,4),repmat(3,4))
dreshape=reshape(d,4,12)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!