필터 지우기
필터 지우기

collapse 3d data set to 2d data set using indexer for 3rd dimension without loops

조회 수: 2 (최근 30일)
I have a 3d data set. I want to collapse the 3rd dimension of this data set. I have a 2d data set which represents the points i want to keep in the collapsing of the 3d data set. How can I accomplish this without having to use loops? This is how I am doing it now (with loops):
data = rand(100,100,10);
[xx,yy] = meshgrid(1:100,1:100);
zz = round(.03*xx+.05*yy);
zz(zz<1) = 1;
for i = 1:size(data,1)
for j = 1:size(data,2)
collapsedData(i,j) = data(i,j,zz(i,j));
end
end
  댓글 수: 2
Star Strider
Star Strider 2015년 1월 5일
It’s difficult to understand what you are doing because we have no idea what ‘zz’ is or how it relates to ‘data’. If would help if you gave numeric example of ‘data’ and what you want ‘collapsedData’ to be.
Stephen23
Stephen23 2015년 1월 5일
Note that you should not use i or j for the loop variables, as these are both names of the inbuilt imaginary unit .

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

채택된 답변

David Young
David Young 2015년 1월 5일
[yy, xx] = ndgrid(1:size(data,1), 1:size(data,2));
collapsedData = data(sub2ind(size(data), yy, xx, zz))

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by