How to split array into sub arrays?

30 views (last 30 days)
Mekan Nuvryyev
Mekan Nuvryyev on 12 Oct 2021
Commented: Matt J on 18 Oct 2021
Hi, I'm quite new to MATLAB, so please bear with my simple question.
Assume I have an array of A with 91612 entries - 91612x1 double. I want to split it and create multiple non-overlapping arrays [ example: Array_1 (1000x1 double); Array_2 (1000x1 double) ... Array_N (612x1 double) ]. Notice that last array has only 612 entries, as the original array A has 91612 entries.
Questions:
  1. How can I do this via loop method?
  2. How can I do this without loop method?
  3. I have a feeling that creating individual variables is not most efficient method, so I presume that the answer will be to create a table (T) with array as each element. Hence the question, how can I create an array (M) where each element represents the mean of the each cell in table T? Ex: M(n) = mean(T(n)) , where n represend respective index of the total created cells in the table T? Will it be something like this:
for n=1:length(T)
M(n) = mean( T(n) );
end?
Apologies beforehand as I cannot upload the original file, hence please, as an example, just create random non-even array, i.e. 124 (with window of 10), 3214 (with window of 100) or something like this. Thank you, kindly, for your assistance!
Best Regards,
Mekan

Accepted Answer

Stephen
Stephen on 18 Oct 2021
Edited: Stephen on 18 Oct 2021
"I have a feeling that creating individual variables is not most efficient method..."
Creating lots of individual variables would be slow, complex, difficult to debug, and very inefficient.
"...so I presume that the answer will be to create a table (T) with array as each element"
Using a cell array is simpler, for example using mat2cell and some basic modulo maths (no loops required):
A = rand(124,1) % random data
A = 124×1
0.9858 0.9376 0.4004 0.3453 0.2916 0.7234 0.5530 0.9021 0.7499 0.2642
N = 10; % window size
X = size(A,1)-1;
Y = [N*ones(1,fix(X/N)),1+rem(X,N)]
Y = 1×13
10 10 10 10 10 10 10 10 10 10 10 10 4
C = mat2cell(A,Y,1)
C = 13×1 cell array
{10×1 double} {10×1 double} {10×1 double} {10×1 double} {10×1 double} {10×1 double} {10×1 double} {10×1 double} {10×1 double} {10×1 double} {10×1 double} {10×1 double} { 4×1 double}
Part three of your question could be approached using CELLFUN:
M = cellfun(@mean,C)
M = 13×1
0.6153 0.4236 0.4151 0.7397 0.4655 0.4444 0.5167 0.4445 0.5626 0.4495
or a simple loop:
N = numel(C);
M = nan(N,1);
for k = 1:N
M(k) = mean(C{k});
end
M
M = 13×1
0.6153 0.4236 0.4151 0.7397 0.4655 0.4444 0.5167 0.4445 0.5626 0.4495
  1 Comment
Mekan Nuvryyev
Mekan Nuvryyev on 18 Oct 2021
Hi, Stephen,
Thank you, kindly, for your answer and in-depth explanation! I believe you have answered all my questions to my satisfaction. Thank you!
Best Regards,
Mekan

Sign in to comment.

More Answers (1)

Matt J
Matt J on 12 Oct 2021
Edited: Matt J on 12 Oct 2021
The way you would do this is to convert the vector to a cell array. There is no way to do it without a loop. You can use mat2tiles from the File Exchange to abbreviate the task
Example:
A=rand(91612,1);
out = mat2tiles( A ,[1000,1])
out = 92×1 cell array
{1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double} {1000×1 double}
  5 Comments
Matt J
Matt J on 18 Oct 2021
All arrays are processed using loops.
The question purported to avoid Mcoded for-loops, which mat2cell has...

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!