for x=1:1:rows
for y=1:1:cols
RE_p(y,x) = RE(y,x,bestori_map(y,x));
RO_p(y,x) = RO(y,x,bestori_map(y,x));
end
end
- RE, RO: arrays of size cols*rows*8;
- bestori_map: array of size cols*rows;
what is the best way (and/or fastest) to vectorize this piece of code withouth the need of for cycles ?
Thank you

댓글 수: 2

Cedric
Cedric 2013년 11월 7일
편집: Cedric 2013년 11월 7일
What are RE, RO, and bestori_map?
VisLab
VisLab 2013년 11월 8일
편집: VisLab 2013년 11월 8일
RE, RO - arrays of size cols*rows*8
bestori_map - array of size cols*rows

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 11월 8일

0 개 추천

s = size(RE);
[ii,jj] = ndgrid(1:s(1),1:s(2));
ij = sub2ind(s,ii(:),jj(:),bestori_map(:));
RE_p = reshape(RE(ij),s(1:2));
RO_p = reshape(RO(ij),s(1:2));

댓글 수: 1

VisLab
VisLab 2013년 11월 8일
Works like a charm!
If you'd care to briefly explain the process it would be awesome.
Thank you very much :)

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 11월 7일
편집: Andrei Bobrov 2013년 11월 8일

1 개 추천

[X, Y] = ndgrid(1:rows, 1:cols);
X = X(:);
Y = Y(:);
Z = reshape( bestori_map(1:rows,1:cols), [], 1);
linidx = sub2ind(size(RE), X,Y, Z);
RE_p(1:rows,1:cols ) = reshape(RE(linidx),rows, cols );
linidx = sub2ind(size(RO), X, Y, Z);
RO_p(1:rows,1:cols) = reshape(RO(linidx),rows, cols, );
Note: if RE and RO are the same size then you can re-use linidx instead of re-calculating it.

댓글 수: 4

Sean de Wolski
Sean de Wolski 2013년 11월 7일
sub2ind()?
VisLab
VisLab 2013년 11월 8일
편집: VisLab 2013년 11월 8일
sub2idx() is undefined, and sub2ind() gives me wrong results. Also, RE and RO are both the same size :).
Can you help me a little bit more ? ty
Andrei Bobrov
Andrei Bobrov 2013년 11월 8일
Hi VisLab! Your RO and RE - functions or arrays?
VisLab
VisLab 2013년 11월 8일
arrays of cols*rows*8 size (RE,RO) and cols*rows (RE_p,RO_p, bestori_map), respectively.

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

카테고리

도움말 센터File Exchange에서 Display Image에 대해 자세히 알아보기

태그

질문:

2013년 11월 7일

편집:

2013년 11월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by