How do I simplify this command by avoiding cycle

At the end,I accept this code
destrows = repmat(ax, 1, 1,3);
destcols = repmat(ay, 1, 1,3);
[srcrows, srccols, srcpages] = ndgrid(1:iH, 1:iW, 1:3);
outImg(sub2ind(size(outImg), destrows, destcols, srcpages)) = tempImg(sub2ind(size(tempImg), srcrows, srccols, srcpages));
---
for x=1:iH
for y=1:iW
outImg(ax(x,y),ay(x,y),:)=tempImg(x,y,:);
end
end
Is it possible to use bsxfun?

 採用された回答

Guillaume
Guillaume 2016 年 6 月 6 日

0 投票

bsxfun wouldn't work for this. sub2ind will, but it won't be pretty:
numpages = size(tempImg, 3);
destrows = repmat(ax(1:iH, 1:iW), 1, 1, numpages);
destcols = repmat(ay(1:iH, 1:iW), 1, 1, numpages);
destpages = repelem(permute(1:numpages, [1 3 2]), iH, iW);
[srcrows, srccols, srcpages] = ndgrid(1:iH, 1:iW, 1:numpages);
outImg(sub2ind(size(outImg), destrows, destcols, destpages)) = tempImg(sub2ind(size(tempImg), srcrows, srccols, srcpages));

1 件のコメント

KIKi
KIKi 2016 年 6 月 7 日
Thanks, but My Matlab version is R2014b. So I modify your code.
destrows = repmat(ax, 1, 1,3);
destcols = repmat(ay, 1, 1,3);
[srcrows, srccols, srcpages] = ndgrid(1:iH, 1:iW, 1:3);
outImg(sub2ind(size(outImg), destrows, destcols, srcpages)) = tempImg(sub2ind(size(tempImg), srcrows, srccols, srcpages));

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2016 年 6 月 6 日

編集済み:

2016 年 6 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by