Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectorize nested for-loop and if statement

i'm trying to vectorize the following nested loop, so I don't have to plot the values in a loop:

for i=1:size(validMaskX,1)
   for j=1:size(validMaskX,2)
      if( validMaskX(i,j) )
         plot(ah, [dataX(i,j) dataX(i,j+1)], [dataY(i,j) dataY(i,j+1)], 'g-')               
      end
    end
 end
  • size(validMaskX) = 45x44
  • size(dataX)=size(dataY)=45x45

Any suggestions on how to do this?

like image 544
K. Grouleff Avatar asked Oct 19 '22 21:10

K. Grouleff


1 Answers

With

vind=find(validMaskX);
vindn = vind + size(validMaskX, 1);

you can find the valid points and the second indices. Then, you can plot with

plot(ah, [dataX(vind), dataX(vindn)], [dataY(vind), dataY(vindn)], 'g-'); 

If you want only one plot object (which would make rendering much faster), consider

dx = [dataX(vind), dataX(vindn), nan(numel(vind), 1)]';
dy = [dataY(vind), dataY(vindn), nan(numel(vind), 1)]';
plot(ah, dx(:), dy(:), 'g-');
like image 152
zeeMonkeez Avatar answered Oct 21 '22 15:10

zeeMonkeez