필터 지우기
필터 지우기

any fast funcitons to do this triple for loop?

조회 수: 2 (최근 30일)
jenka
jenka 2012년 5월 3일
I have this code. Is there any way to make it faster. My data is huge. Thanks. I am simple restoring everthing into vector instead of 3D array:
ii = 1
for z = 1:data_size(3)
for y = 1:data_size(2)
for x = 1:data_size(1)
data_tmp(ii) = data(x,y,z);
ii = ii+1;
end
end
end
  댓글 수: 1
Daniel Shub
Daniel Shub 2012년 5월 3일
If your data is huge, you will definitely want to preallocate data_tmp.

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

답변 (1개)

Daniel Shub
Daniel Shub 2012년 5월 3일
I believe
data_tmp = data(:);
should work, although the collapsing across dimension might not perfectly match your loops.
Edit: A simple check
data = reshape(1:8, 2, 2, 2)
data_size = size(data);
ii = 1;
for z = 1:data_size(3)
for y = 1:data_size(2)
for x = 1:data_size(1)
data_tmp(ii) = data(x,y,z);
ii = ii+1;
end
end
end
isequal(data_tmp, data(:))
reveals that my solution does not match your solution. But,
isequal(data_tmp, data(:)')
does match. So the answer is
data_tmp = data(:)';
  댓글 수: 2
jenka
jenka 2012년 5월 3일
what do you mean: "collapsing across dimension might not perfectly match your loops"?
It is essential for my goal that the things are stored into my vector the way I put them through my loop (i.e. x first, then y, then z). Also, my starting point should be lower left corner. So I need some flipping?
Daniel Shub
Daniel Shub 2012년 5월 3일
See my edit.

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

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by