简体   繁体   English

Matlab:沿3D数组的第一维进行操作

[英]Matlab: Operations along the first dimension of a 3D array

I have a 3D array M(d*d,m,n). 我有一个3D数组M(d * d,m,n)。 For each d*d vector of M (ie vectors of the first dimension), I split it into d parts and take the sum of each part to form a new vector (of size d). 对于M的每个d * d向量(即第一维的向量),我将其分成d个部分,并取每个部分的总和形成一个新的向量(大小为d)。 For example, if u is a vector along the first dimension of M, then it will be replaced by the vector v, computed by: 例如,如果u是沿着M的第一个维度的向量,则它将被向量v代替,该向量由以下公式计算:

v = sum(reshape(u,d,d))';

For the moment I use a loop as following, but I think there should be a much faster way to do it. 目前,我使用如下循环,但是我认为应该有一种更快的方法。

N = zeros(d,m,n)
for i=1:m
    for j=1:n
        N(:,i,j) = sum(reshape(M(:,i,j),d,d))'; %//'
    end
end

Thank you so much for any suggestions! 非常感谢您的任何建议!

尝试这个 -

N = reshape(sum(reshape(M,d,[])),d,m,n)

我可能没有正确理解问题,但这是您要寻找的吗?

N=squeeze(sum(reshape(M,[d,d,size(M,2),size(M,3)])))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM