How to split array into sub arrays?

33 ビュー (過去 30 日間)
Mekan Nuvryyev
Mekan Nuvryyev 2021 年 10 月 12 日
コメント済み: Matt J 2021 年 10 月 18 日
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

採用された回答

Stephen
Stephen 2021 年 10 月 18 日
編集済み: Stephen 2021 年 10 月 18 日
"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 件のコメント
Mekan Nuvryyev
Mekan Nuvryyev 2021 年 10 月 18 日
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

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2021 年 10 月 12 日
編集済み: Matt J 2021 年 10 月 12 日
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 件のコメント
Matt J
Matt J 2021 年 10 月 18 日
All arrays are processed using loops.
The question purported to avoid Mcoded for-loops, which mat2cell has...

サインインしてコメントする。

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by