How do I output my data as a list?

조회 수: 1 (최근 30일)
Toni Addison
Toni Addison 2013년 11월 18일
댓글: Azzi Abdelmalek 2013년 11월 18일
I have a matrix and I am told to split my one column matrix into two equal matrics; For example if D=[1 5 2 4] then my output would be A11=[1 5] A12=[2 4]. I have coded the following function below my issue is my last A12 matrix =[0 0 2 4] instead of just [2 4]. How do I fix this? thanks
[A11,A12,]=SplitMatrix( D )
[r c]=size(D);
if r==1 && Mod(c,2)==0
for i=1:r
for j=1:c/2
A11(j)=D(i,j)
end
for j=((c/2+1):c)
A12(j)=D(i,j)
end
end

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 18일
A11=D(1:2)
A12=D(2+1:end)
  댓글 수: 2
Toni Addison
Toni Addison 2013년 11월 18일
Thanks for responding, what if I have more than 4 columns lets say 10. I am trying to write the function to accept any size. How do I make it general for length of columns?
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 18일
n=numel(D)
m=ceil(n/2)
A11=D(1:m)
A12=D(m+1:end)

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

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by