为了优化我的代码我做了内存分配,但由于内存分配,我收到以下错误: 从单元格转换为双精度时发生以下错误:
使用double时出错:
无法从单元格转换为double:
patches(ind)= arrayfun(@(ind) reshape(neigh(:,ind),[3 1 3]), 1:numel(ind), 'uni', 0);%create cell arrays for each patch
代码如下:
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
在没有内存分配的情况下,代码执行的时间非常长(接近3小时)。所以我需要尽可能高效。