简体   繁体   中英

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

I have a 3D array 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). For example, if u is a vector along the first dimension of M, then it will be replaced by the vector v, computed by:

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)])))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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