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
Any suggestions on how to do this?
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-');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With