Split array into subarrays
222 views (last 30 days)
Show older comments
David Peña
on 26 Mar 2022
Edited: David Goodmanson
on 27 Mar 2022
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 Comments
Accepted Answer
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}
0 Comments
More Answers (2)
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.
0 Comments
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).'
data_to_use = reshape(raw_data,[],4)
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!