Operations along the first dimension of a 3D array

조회 수: 3 (최근 30일)
f10w
f10w 2014년 5월 29일
편집: Matt J 2014년 5월 29일
I have a 3D array M(d*d,m,n). For each d*d vector of M (i.e. 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!

채택된 답변

Matt J
Matt J 2014년 5월 29일
편집: Matt J 2014년 5월 29일
Looks like M should have been a 4D array from the beginning,
M=reshape(M,d,d,m,n);
N=sum(M,2);
  댓글 수: 1
Matt J
Matt J 2014년 5월 29일
편집: Matt J 2014년 5월 29일
Khue Commented:
Thanks. But N=sum(M,2) will give a 4D array, right? I got this answer:
N = squeeze(sum(reshape(M,d,d,m,n)))
The idea is the same as yours.
Thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by