i need to reshape rows that are in complicated manner!

I have data in rows which is a bit complicated to arrange i need a code that will arrange in columns.
Example:
If A=
[1 3 5 7 9
2 4 6 8 10
1 3 5 7 9
2 4 6 8 10
1 3 5 7 9
2 4 6 8 10
1 3 5 7 9
2 4 6 8 10]
thats what data looks like. 1 and 2 rows contains data of same category so i want them to be arranged in a single column,same goes for the other three pairs in total i need 4 columns. but the reshaping command will put the 2nd column 1st value exactly after last value.

 Accepted Answer

The number of rows in A must be even.
n = size(A,2);
B = reshape(A.,2*n,[]);
B = B(ceil((1:2*n)/2)+n*mod(0:2*n-1,2),:);

3 Comments

Fantastic it worked Thankyou sooo much:) im beginner so can you please explain me this part of the code? how it works?
B = B(ceil((1:2*n)/2)+n*mod(0:2*n-1,2),:);
In the quantity
p = ceil((1:2*n)/2)+n*mod(0:2*n-1,2)
what we need is
p = [1,1+n,2,2+n,3,3+n,...]
in order to do the proper rearrangement. The first term will be:
ceil((1:2*n)/2) = [1,1,2,2,3,3,...]
and the second term will give:
n*mod(0:2*n-1,2) = [0,n,0,n,...]
Adding these together gives the p that we require.

Sign in to comment.

More Answers (2)

Easy.
A = reshape(A',10,4);
A =
1 1 1 1
3 3 3 3
5 5 5 5
7 7 7 7
9 9 9 9
2 2 2 2
4 4 4 4
6 6 6 6
8 8 8 8
10 10 10 10
Output should look this
A=[1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10]
C1 is a combination of R1 and R2 same goes for others.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!