how to split data

조회 수: 43 (최근 30일)
Tino
Tino 2019년 6월 4일
답변: Star Strider 2019년 6월 4일
rx = [1
3
4
5
6
7
8
9
2
6]
Please how can I divide this data above to half to get x and y
x = [ 1 3 4 5 6 ]
y = [ 7 8 9 2 6]
thanks
  댓글 수: 2
madhan ravi
madhan ravi 2019년 6월 4일
What if it has 11 elements then ??
Tino
Tino 2019년 6월 4일
you ignore the last element

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

답변 (2개)

Stephen23
Stephen23 2019년 6월 4일
편집: Stephen23 2019년 6월 4일
Why not just use basic MATLAB indexing?:
>> x = rx(1:5)
x =
1
3
4
5
6
>> y = rx(6:10)
y =
7
8
9
2
6
If you really want the output to be horizontal, simply add a transpose:
>> x = rx(1:5).'
x =
1 3 4 5 6

Star Strider
Star Strider 2019년 6월 4일
Another option:
M = reshape(rx(1:2*fix(numel(rx)/2)), fix(numel(rx)/2), []);
x = M(:,1)'
y = M(:,2)'
This automatically discards elements at the end that result in the length of ‘rx’ not being a multiple of 2, and produces the same result if ‘rx’ is a row or column vector. It only works correctly if ‘rx’ is a vector.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by