How to split array into sub arrays?

조회 수: 211 (최근 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

채택된 답변

Stephen23
Stephen23 2021년 10월 18일
편집: Stephen23 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
Stephen23
Stephen23 2021년 10월 18일
편집: Stephen23 2021년 10월 18일
"mat2cell.m has several for loops inside it."
All arrays are processed using loops.
"There is no way to build cell arrays and struct arrays that avoid for-loop speed limitations."
How can I demonstrate these "speed limitations" and see their effects on my code? I see references to these mythical speed issues, but so far no convincing examples or even explanations of how to demonstrate them. Or for that matter, any theoretical explanation why MATLAB's for-loops should be slower than some reasonably expected finite speed.
Matt J
Matt J 2021년 10월 18일
All arrays are processed using loops.
The question purported to avoid Mcoded for-loops, which mat2cell has...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by