How to slice an array across multiple dimensions with two limit arrays?
5 次查看(过去 30 天)
显示 更早的评论
I'm interested in taking an arbitrary N-dimensional array, and two 1xN vectors, and using the two vectors as start/stop indices to slice the array, but I'm not sure how. Here's what I mean:
Let X be an N-dimensional array.
Let A and B be 1xN vectors like so:
A=[a1, a2, ..., aN]
B=[b1, b2, ..., bN]
How do I slice X such that I get this:
Y = X(a1:b1, a2:b2, ..., aN:bN)
I tried X(A:B), but that doesn't work - not even sure what that operation is doing, but it isn't doing what I want. I tried to think of some way to do it with logical indexing, but nothing simple came to mind. I could slice the array one dimension at a time in a loop, but it seems like there must be a one-liner. Thanks!
0 个评论
采纳的回答
Bruno Luong
2022-4-22
编辑:Bruno Luong
2022-4-22
This should work
X=rand(6,6,6);
A=[1 2 3]; B=[6 5 4];
cidx = arrayfun(@(i1,i2) i1:i2, A, B, 'unif', 0);
Y = X(cidx{:})
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!