-2

So I want to create a loop in MATLAB where I can retrieve the x,y pairs.

So far, I have two arrays:

x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];

I would like to create a for loop where I can retrieve pairs (x1,y1), then (x2, y1), then (x2, y2), (x1, y2), and lastly (x1,y1) once again.

2 Answers2

1

This is a trivial loop:

x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
for index = 1:numel(x)
   pair = [ x(index), y(index) ];
end
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
0

In Matlab it is great to be able to avoid loops.

You can build a matrix from your two vectors:

xy = [x;y];

Now every column of xy is a pair. You can then do:

for col_index = 1 : size(xy,2)
    xy(:, col_index)  % whatever you want to do here
end
TallBrianL
  • 1,230
  • 1
  • 9
  • 14