How to split a matrix in two halves?

조회 수: 19 (최근 30일)
Emanuele Joy
Emanuele Joy 2018년 5월 17일
댓글: Emanuele Joy 2018년 5월 17일
For example, if I have matrix: A = [5, 9, 25, 2, 21, 36];
and I want matrices:
B = [5, 9, 25]; C = [2, 21, 36];
What command do I need to use to split it into two halves? I originally have a matrix of size 1x100001 so I can't manually make two matrices like what I did here lol

채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 5월 17일
편집: Andrei Bobrov 2018년 5월 17일
n = ceil(numel(A)/2);
B = A(1:n)
C = A(n+1:end);
or
n = ceil(numel(A)/2);
BandC = mat2cell(A(:)',1,[n,numel(A)-n]);
  댓글 수: 1
Emanuele Joy
Emanuele Joy 2018년 5월 17일
This works like a dream. Thank you!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by