In order to optimize my code I did memory allocation but due to memory allocation I get the following error: The following error occurred converting from cell to double:
Error using double:
Conversion to double from cell is not possible:
patches(ind)= arrayfun(@(ind) reshape(neigh(:,ind),[3 1 3]), 1:numel(ind), 'uni', 0);%create cell arrays for each patch
The code is as follows:
M1=4;
N1=4;
patches=zeros(1,80000);
for i=1:nframes
Apad = padarray(I2(:,:,:,i), floor([M1 N1]/2), 'replicate');%To grab pixel neighbourhoods along the borders of the image padding is done
B =im2col_rgb(Apad,[3 3]);%sliding neighbourhood Im2col is used to transform each pixel neighbourhood into a single column. Each block is choosen on a column basis
%in our case a 3*3 neighbourhood is found for each pixel
%and stored in a column im2col_rgb is for color images
for y=1:240
for x=1:320
for z=1:3
ind = sub2ind([size(I2(:,:,z,i),1) size(I2(:,:,z,i),2)], y, x);%x,y co-ordinates are converted to linear indices to easily select the neighbourhood
neigh = B(:,ind);%select neighbourhood
patches(ind)= arrayfun(@(ind) reshape(neigh(:,ind),[3 1 3]), 1:numel(ind), 'uni', 0);%create cell arrays for each patch
end
In the absence of memory allocation the code takes a really really long time (close to 3 hrs) to execute. So I need to make it as efficient as possible.