Info
此问题已关闭。 请重新打开它进行编辑或回答。
Vectorized code not faster
1 次查看(过去 30 天)
显示 更早的评论
Hello, i had the following code:
for i=1:length(rho);
for j=i:length(theta);
PolarImage(j,i)=diag(CartImage(Y(i,j),X(i,j)));
end
end
Elapsed time is 0.019816 seconds
I changed it to:
for i=1:length(rho);
PolarImage(:,i)=diag(CartImage(Y(i,:),X(i,:)));
end
Elapsed time is 0.093083 seconds
I do not understand, why the vectorized code is a lot slower than the previous one. does anyone know an efficient way, for faster calculation? thanks ahead, Bart
3 个评论
Walter Roberson
2015-11-20
What value are you expecting diag(CartImage(Y(i,j),X(i,j))) to be that is different from CartImage(Y(i,j),X(i,j)) ?
回答(1 个)
Walter Roberson
2015-11-20
for i = 1:length(rho)
idx = sub2ind(size(CartImage), Y(i,:), X(i,:));
PolarImage(:,i) = CartImage(idx);
end
Or to be more efficient:
nrow = size(CartImage,1);
for i = 1:length(rho)
PolarImage(:,i) = CartImage( (X(i,:) - 1) * nrow + Y(i,:) );
end
2 个评论
Walter Roberson
2015-11-20
nrow = size(CartImage,1);
PolarImage = CartImage( (X - 1) * nrow + Y ) .';
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!