How to split array into sub arrays?
213 ビュー (過去 30 日間)
古いコメントを表示
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:
- How can I do this via loop method?
- How can I do this without loop method?
- 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
0 件のコメント
採用された回答
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
N = 10; % window size
X = size(A,1)-1;
Y = [N*ones(1,fix(X/N)),1+rem(X,N)]
C = mat2cell(A,Y,1)
Part three of your question could be approached using CELLFUN:
M = cellfun(@mean,C)
or a simple loop:
N = numel(C);
M = nan(N,1);
for k = 1:N
M(k) = mean(C{k});
end
M
その他の回答 (1 件)
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])
5 件のコメント
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
2021 年 10 月 18 日
All arrays are processed using loops.
The question purported to avoid Mcoded for-loops, which mat2cell has...
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!