Search code examples
matlabloopsvectorizationedge-detection

Subscripted assignment dimension mismatch after vectorizing a loop


I tried turning this:

for r=1:fsize
     for c=1:fsize
         mask(r,c) = exp(-(((r-centre)^2+(c-centre)^2)/2*(sigma^2)));
     end
 end

into

mask(1:fsize,1:fsize) = exp(-(((1-centre:fsize-centre).^2.+(1-centre:fsize-centre).^2)./2.*(sigma.^2)));

but I now get the error "Subscripted assignment dimension mismatch".

My understanding is that this has something to do with the indexing in the two arrays not being matched, but that doesn't seem to be the case here?


Solution

  • use meshgrid for that,

    [r c]=meshgrid(1:fsize);
    mask = exp(-(((r-centre).^2+(c-centre).^2)/2*(sigma^2)));   
    

    this assumes that centre and sigma are scalars.