Split array into subarrays

조회 수: 223 (최근 30일)
David Peña
David Peña 2022년 3월 26일
편집: David Goodmanson 2022년 3월 27일
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?

채택된 답변

Walter Roberson
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}

추가 답변 (2개)

David Goodmanson
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.

Voss
Voss 2022년 3월 26일
How about if you put each 50x1 array into its own column of a 50x4 matrix:
raw_data = (1:200).'
raw_data = 200×1
1 2 3 4 5 6 7 8 9 10
data_to_use = reshape(raw_data,[],4)
data_to_use = 50×4
1 51 101 151 2 52 102 152 3 53 103 153 4 54 104 154 5 55 105 155 6 56 106 156 7 57 107 157 8 58 108 158 9 59 109 159 10 60 110 160

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by