Vectorized code not faster
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
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 日
Is CartImage a matrix or a function? If it is a matrix then CartImage(Y(i,j),X(i,j)) would be a single value and diag() of a single value is just going to be the value, so it is not clear why you would bother. If CartImage is a function then we need more information about what it computes.
Bart van Essen
2015 年 11 月 20 日
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 件のコメント
Bart van Essen
2015 年 11 月 20 日
編集済み: Bart van Essen
2015 年 11 月 20 日
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!