How can I vectorize/LUT this for loop ?

1 回表示 (過去 30 日間)
Louis
Louis 2013 年 2 月 12 日
Hello,
I have been dabbling trying to make a look-up table mapping for this tedious for-loop but I wasn't successful. Here is the code and explanation of what I am trying to do:
for kk=1:size(aa,1)
imageModified(aa(kk,1),aa(kk,2))=img(aa(kk,3),aa(kk,4));
end
My 'kk' is in order of millions for this loop is somehow time consuming.
Basically, I have a matrix called 'aa'.
'aa' is some Nx4 matrix where each row has mapping values.
What I want to do is going through row by row of matrix 'aa' and map image value 'img' to its corresponding place in image 'imageModified'.
For example,
Let's say aa=[ 50 40 60 70 ; 2 56 28 10 ; .... ]
So what I want to do is: map img(50,40) value to location imgModified(60,70). Map map img(2,56) value to location imgModified(28,10). And do that for 'aa' N rows.
Thank you in advance,

採用された回答

Sven
Sven 2013 年 2 月 12 日
編集済み: Sven 2013 年 2 月 12 日
Hi Louis,
This one's got a nice solution using sub2ind that lets you do the whole thing at once without a loop:
img = rand(100,100)
imgModified = zeros(size(img))
aa = [ 50 40 60 70 ; 2 56 28 10]; % etc
imgInds = sub2ind(size(img),aa(:,1),aa(:,2))
modInds = sub2ind(size(img),aa(:,3),aa(:,4))
imgModified(modInds) = img(imgInds)
Does that end up with what you intended?
  2 件のコメント
Louis
Louis 2013 年 2 月 12 日
編集済み: Louis 2013 年 2 月 12 日
Awesome. Thank you very much, Sven. Time down from 22 seconds to 0.2 seconds :)
Sven
Sven 2013 年 2 月 12 日
No problems, you get a vote for writing a nice clear question with example code :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeometric Transformation and Image Registration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by