How to use arrayfun to create an array whose value are relating to index?
古いコメントを表示
I'm now doing a project of image processing, but I have some troubles when using the function arrayfun(). Let me show the core part of my code first. I create an array named resizedImage.
resizedImage = zeros(dr, dc, 'uint8');
Then, I attempt to change the value of each entry of this array, so I use for-loop to achieve this.
for i = 1 : dr
for j = 1 : dc
resizedImage(i, j) = pixel_replication(img, i * dist_r, j * dist_c);
end
end
function val = pixel_replication(img, x, y)
% do something
end
However, for-loop is very time-consuming, so I try to use arrayfun() to make my code more efficient. Anyone can help me?
3 件のコメント
Dyuman Joshi
2023 年 9 月 20 日
arrayfun is not going to be faster than the for loop, at best it might match it.
Your best bet would be to see if the function pixel_replication() can be optimized.
Can you attach your code? Use the paperclip button to attach.
Duen Chian
2023 年 9 月 20 日
dpb
2023 年 9 月 20 日
for i = 1 : dr
for j = 1 : dc
resizedImage(i, j) = pixel_replication(img, i * dist_r, j * dist_c);
The order of the loops above is processing the array in row major, not column major order; rearranging those as
for j = 1 : dc
for i = 1 : dr
resizedImage(i, j) = pixel_replication(img, i * dist_r, j * dist_c);
could help some, but as @Dyuman Joshi notes, the time consumed will be in the function so whatever can be done there would be the place to look.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Image Arithmetic についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!