我有一个包含A(361,1)
矩阵的单元格361 x 3D
。矩阵的第一维和第二维是相同的,但第三维的长度是变化的。
所以单元格A看起来像:
A={(464x201x31);(464x201x28);(464x201x31);(464x201x30)....}
我想通过循环从这个单元格中取回矩阵。我尝试了以下解决方案:
for i=1:361;
M(i)=cell2mat(A(i));
end
但是我收到了以下错误:
订阅的分配维度不匹配。
答案 0 :(得分:2)
1。如果您希望分别从单元格阵列访问每个3D数组,您可以始终使用A{i}
来获取每个3D矩阵。
示例:
%// Here i have taken example cell array of 1D matrix
%// but it holds good for any dimensions
A = {[1,2,3], [1,2,3,4,5], [1,2,3,4,5,6]};
>> A{1}
ans =
1 2 3
2。相反,如果你想连接所有这些3D矩阵到一个三维矩阵,这里有一种方法
out = cell2mat(permute(A,[1 3 2])); %// assuming A is 1x361 from your example
或
out = cell2mat(permute(A,[3 2 1])); %// assuming A is 361x1
3. 相反,如果你想 NaN pad 他们获得4D矩阵,
maxSize = max(cellfun(@(x) size(x,3),A));
f = @(x) cat(3, x, nan(size(x,1),size(x,2),maxSize-size(x,3)));
out = cellfun(f,A,'UniformOutput',false);
out = cat(4,out{:});
示例运行:
>> A
A =
[3x4x2 double] [3x4x3 double] [3x4x4 double]
>> size(out)
ans =
3 4 4 3
%// note the first 3 size. It took the max size of the 3D matrices. i.e 3x4x4
%// Size of 4th dimension is equal to the no. of 3D matrices
您可以通过out(:,:,:,i)