Reshape does not Use the Columns I Want

So, I have a matrix x=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
I want to create a 2-column matrix with indefinite numbers of rows and to do that I use
reshape(x,[],2). The result I get is
1 3
5 7
9 11
13 15
17 19
2 4
6 8
10 12
14 16
18 20
But I'd like to have
1 2
5 6
9 10
13 14
17 18
3 4
7 8
11 12
15 16
19 20
What can I do?

 Accepted Answer

For your posted example, simply
[x(:,1:2);x(:,3:4)]
But this begs the question, what is your real problem and what sizes can x be? Always 4 columns or any number of even columns or ???

3 Comments

I'm trying to get coordinates on 2-dimensional. So, x can be any size of even columns and I want to transform it into a 2-column matrix. Each row is a point in the x-y axis. For my example, 1 and 2 is the point (1,2) and 3 4 is the point (3,4).
If I understand you correctly, then
X = x(:,1:2:end);
Y = x(:,2:2:end);
result = [X(:),Y(:)];

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!