How to reshape matrix in Matlab

조회 수: 4 (최근 30일)
Zhan
Zhan 2017년 4월 3일
답변: Star Strider 2017년 4월 3일
I have matrix A as follows:
A = [98 3 2 7
99 4 1 4
100 5 12 3
101 6 4 2
102 7 6 1
];
I want to transfer matrix A to the following format:
B = [98 3
99 4
100 5
101 6
102 7
98 2
99 1
100 12
101 4
102 6
98 7
99 4
100 3
101 2
102 1
];
% from matrix A, col 3:4 are written under the second column and first colum is repeated.

답변 (1개)

Star Strider
Star Strider 2017년 4월 3일
This works:
A = [98 3 2 7
99 4 1 4
100 5 12 3
101 6 4 2
102 7 6 1];
B = [repmat(A(:,1), size(A,2)-1, 1) reshape(A(:,2:end), [],1)]
B =
98 3
99 4
100 5
101 6
102 7
98 2
99 1
100 12
101 4
102 6
98 7
99 4
100 3
101 2
102 1

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by