Split array into subarrays

16 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 26 Mar 2022
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}

More Answers (2)

David Goodmanson
David Goodmanson on 26 Mar 2022
Edited: David Goodmanson on 27 Mar 2022
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 on 26 Mar 2022
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

Community Treasure Hunt

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

Start Hunting!