i need to reshape rows that are in complicated manner!

1 回表示 (過去 30 日間)
Mahrukh Farooq
Mahrukh Farooq 2016 年 12 月 18 日
コメント済み: Mahrukh Farooq 2016 年 12 月 25 日
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.

採用された回答

Roger Stafford
Roger Stafford 2016 年 12 月 19 日
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 件のコメント
Roger Stafford
Roger Stafford 2016 年 12 月 19 日
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.
Mahrukh Farooq
Mahrukh Farooq 2016 年 12 月 25 日
Thanks a lot again!

サインインしてコメントする。

その他の回答 (2 件)

John D'Errico
John D'Errico 2016 年 12 月 18 日
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

Mahrukh Farooq
Mahrukh Farooq 2016 年 12 月 19 日
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.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by