Split array into subarrays
조회 수: 223 (최근 30일)
이전 댓글 표시
Hello everyone and thank you in advanced,
I need to separate a 200x1 double array into 4 different 50x1 double arrays, how can I do that
I'm receiving data from an OBR and this data comes in arrays of 50x1 double. The problem is that so I can receive the 4 of them I need to save them all together in the same array and then separate it into 4 different arrays.
The problem i have is that I`m not able to sepparete the array into the 4 smaller arrays. Could anyone help me?
댓글 수: 0
채택된 답변
Walter Roberson
2022년 3월 26일
X = your 200 x 1 array
A = X(1:50);
B = X(51:100);
C = X(101:150);
D = X(151:200);
or, typically better,
X = your 200 x 1 array
A = mat2cell([50 50 50 50], 1);
Now the arrays are A{1}, A{2}, A{3}, A{4}
댓글 수: 0
추가 답변 (2개)
David Goodmanson
2022년 3월 26일
편집: David Goodmanson
2022년 3월 27일
Hi David,
if A is 200x1 the easiest way to do that is
B = reshape(A,50,4)
so now B is a matrix whose four columns are each 50x1. (This assumes that that the four arrays that you want were originally concatenated in end-to-end fashion to make the 200x1 array).
You could make four separate, smaller arrays with a different variable name for each one, but this is discouraged in Matlab since it does not take advantage of Matlab's full capabilities. It's easy to leave the entire set in B and access the third one, say, with B(:,3) which pulls out the third column.
댓글 수: 0
Voss
2022년 3월 26일
How about if you put each 50x1 array into its own column of a 50x4 matrix:
raw_data = (1:200).'
data_to_use = reshape(raw_data,[],4)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!