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!