I built a 1*5 cell named N and need to copy an image (img)matrix into each entry in it what should I do? This is what I came up with but it doesn't work....
I'm trying to avoid for loops so my code will be faster.
function newImgs = imresizenew(img,scale) %scale is an array contains the scaling factors to be upplied originaly an entry in a 16*1 cell
N = (cell(length(scale)-1,1))'; %scale is a 1*6 vector array
N(:,:) = mat2cell(img,size(img),1); %Now every entry in N must contain img, but it fails
newImgs =cellfun(@imresize,N,scale,'UniformOutput', false); %newImgs must contain the new resized imgs
end

 채택된 답변

Guillaume
Guillaume 2019년 3월 11일

0 개 추천

If I understood correctly,
function newImgs = imresizenew(img, scale)
newImgs = arrayfun(@(s) imresize(img, s), scale, 'UniformOutput', false);
end
Note that while more compact and more reliable than loops, arrayfun and cellfun are often slower (due to the extra cost of an anonymous function call). In any case, they're just loops in disguise, so using them "to avoid for loops so my code will be faster" won't work.
The loop equivalent:
function newImgs = imresizenew(img, scale)
newImgs = cell(size(scale));
for scaleiter = 1:numel(scale)
newImgs{scaleiter} = imresize(img, scale(scaleiter));
end
end

추가 답변 (0개)

제품

릴리스

R2016b

질문:

2019년 3월 11일

댓글:

2019년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by