How do i make this matrix simply

I am looking for an easy way to make this Nx2 matrix starting at [0 1]. Every other line the numbers are the inverse of the previous. See example below. I would like to be able to make this array in a simple way in one line without a loop. Either one of the matrix below would work.
x=[0 1;1 0;2 3 ;3 2;4 5;5 4]
OR
x=[0 1;0 1;2 3;2 3;4 5;4 5;]

 Accepted Answer

Stephen23
Stephen23 on 10 Jan 2018
Edited: Stephen23 on 10 Jan 2018
>> N = 5;
>> X = reshape(repmat([0:2:N-1,1:2:N],2,1),[],2)
X =
0 1
0 1
2 3
2 3
4 5
4 5
>> X(2:2:end,[1,2]) = X(2:2:end,[2,1])
X =
0 1
1 0
2 3
3 2
4 5
5 4

1 Comment

This is exactly what i was looking for. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 10 Jan 2018

Commented:

on 10 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!