5

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?

2
  • Not tested, but does vind=find(validMaskX); vindn = vind + size(validMaskX, 1); plot(ah, [dataX(vind), dataX(vindn)], [dataY(vind), dataY(vindn)] ); work? That's assuming validMaskX and dataX and dataY have the same number of rows. Commented Dec 2, 2015 at 12:32
  • Do you want all the lines on the same axis? Because your code plots one line at a time, you'll need a loop for the time term. Commented Dec 2, 2015 at 13:01

3 Answers 3

2

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-');
Sign up to request clarification or add additional context in comments.

8 Comments

This is EXACTLY what I needed! Thanks!
At least you changed the variables names... :)
Hey, I posted this as a comment way before you!
But then again, this is pretty standard stuff :)
Hi zeeMokeez, I thought I understood your solution yesterday, but apparently I don't, because when I try to modify the following code, which is more or less the same, as the question I posted yesterday, it dosen't work:
|
1

If you want all the lines together on the figure, you can do that:

ind=find(validMask);
X=[dataX(ind) dataX(ind+45) nan(length(ind),1)];
Y=[dataY(ind) dataY(ind+45) nan(length(ind),1)];
plot(ah,X',Y','g-')

Comments

0

I thought I understood your solution yesterday, but apparently I don't, because when I try to modify the following code according to your answer, it dosen't work: How would you modify this according to your former answer?

for i=1:size(validMaskY,1)
   for j=1:size(validMaskY,2)
      if( validMaskY(i,j) )
      plot(ah, [dataX(i,j) dataX(i+1,j)], [dataY(i,j) dataY(i+1,j)], 'r-')
      end
   end
end

size(dataX)=size(dataY)

8 Comments

Try to change this line: vindn = vind + 1;
A. size(validMaskY,1) is 45, so when i=45 you can not get dataX(i+1,j), it will exceed matrix dimensions. B. It will be much helpful if you can explain what exactly the problem. We can't guess what "your problem" is, and why it didn't solve.
I'll try to explain a bit more: When I try to vectorize the double for loop, I don't get the same figure, as when I run the double for-loop (which is the correct one!). The first question I posted, draw's connects lines between dots in one direction, and the latest post from me draw lines perpendicular to the other lines. The result is a grid where all dots that satisfy validMaskX, validMaskY is connected. I can draw the lines in one direction, but not in the other direction.
I'm looping through a lot of images, so the size of dataX and dataY will vary, but will always equal each other. Size(validMaskX)=[m,n] where size(validMaskY)=[n,m], hope this helps
The original solution hinged on the fact that matrices are indexed in column-major order. @Adiel's comment regarding vindn = vind + 1; should work. Are the differences in your sizes for validMaskX and validMaskY correct?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.