Split array into subarrays
216 次查看(过去 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!