How can i reorder NxM matrix into a 1D array

조회 수: 7 (최근 30일)
Matthew
Matthew 2018년 6월 12일
댓글: Stephen23 2018년 6월 12일
I am looking to take a sample matrix below. The matrix will be large and looking for fast and non looping solution. Thank you.
a=[1 2 3;4 5 6;7 8 9];
reshape it into:
b=[7 8 9 4 5 6 1 2 3];
  댓글 수: 1
Matthew
Matthew 2018년 6월 12일
No the matrix is not always 3 rows. It will likely be much more in both rows and columns.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2018년 6월 12일
Using the reshape (link) function:
b = reshape(a([3 2 1],:)', 1, [])
b =
7 8 9 4 5 6 1 2 3
  댓글 수: 1
Stephen23
Stephen23 2018년 6월 12일
Or generalized for any number of rows:
reshape(a(end:-1:1,:).', 1, [])

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alfonso
Alfonso 2018년 6월 12일
Without looping you can do:
b = [a(end,:), a(end-1,:), a(end-2,:)]; % 1D array
b =
7 8 9 4 5 6 1 2 3
You can repeat the iterations as many rows you have: [a(end-1,:), ... ,a(end-50,:)]

카테고리

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