【发布时间】:2018-06-01 02:29:00
【问题描述】:
我有一个 3D 矩阵 A (size m*n*k),其中 m=纬度、n*经度和 k=时间。
我只需要由逻辑矩阵B (size m*n) 指定的第一维和第二维的特定值,并且只需要向量C (size k) 指定的时间步长。
最后这应该成为一个二维矩阵 D,因为前两个维度将折叠为一个。
最简单的方法是什么? 还有可能在这里将逻辑与线性索引结合起来吗?比如B是逻辑的,C是线性的?
带有 rand 的示例代码:
A=rand(10,10,10);
B=randi([0 1], 10,10);
C=randi([0 1], 10,1);
D=A(B,C) %This would be my approach which doesnt work. The size of D should be sum(B)*sum(c)
另一个没有 rand 的例子:
A=reshape([1:27],3,3,3);
B=logical([1,0,0;1,0,0;0,0,0]);
C=(1,3); %get data from timestep 1 and 5
D=A(B,C);%What I want to do, but doesnÄt work that way
D=[1,19;2,20];%Result should look like this! First dimension is now all data from dimesion 1 and 2. New dimesion 2 is now the time.
【问题讨论】:
标签: matlab multidimensional-array indexing