how to split a 4x3 matrix in half

조회 수: 2 (최근 30일)
Alexya
Alexya 2022년 10월 7일
편집: dpb 2022년 10월 7일
i have a 4x3 matrix that i need to split in half
arr1 = [1 2 3 4;5 6 7 8;9 10 11 12]
i need it to be
[1 2;5 6;9 10]

답변 (3개)

Torsten
Torsten 2022년 10월 7일
If the number of columns of arr1 is divisible by 2:
arr1 = [1 2 3 4;5 6 7 8;9 10 11 12];
arr1_half = arr1(:,1:size(arr1,2)/2)
arr1_half = 3×2
1 2 5 6 9 10
  댓글 수: 1
dpb
dpb 2022년 10월 7일
편집: dpb 2022년 10월 7일
And depending on which way one wants to go, if odd then use either
arr1_half = arr1(:,1:fix(size(arr1,2))/2);
or
arr1_half = arr1(:,1:ceil(size(arr1,2))/2);
Either of those is general to handle both even and odd number of columns once a decision is made as to which way to round, up or down.

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


Ghazwan
Ghazwan 2022년 10월 7일
Newarr1=arr1(:,1:length(arr1(1,:))/2)
%Note that the number of columns has to be even

Image Analyst
Image Analyst 2022년 10월 7일
This is just very basic indexing. To learn other fundamental concepts, invest 2 hours of your time here:

카테고리

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