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?
0 个评论
采纳的回答
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)
d = cat(3,repmat(1,4),repmat(2,4),repmat(3,4))
dreshape=reshape(d,4,12)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!