How to use arrayfun to create an array whose value are relating to index?
Show older comments
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 Comments
Dyuman Joshi
on 20 Sep 2023
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
on 20 Sep 2023
dpb
on 20 Sep 2023
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.
Accepted Answer
More Answers (0)
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!