How to divide an array
2 ビュー (過去 30 日間)
古いコメントを表示
I have an array of 19361 samples. i want to divide it such that each array has 365 samples. 19361 is not perfactly divisible by 365. How can I do that?
2 件のコメント
James Tursa
2019 年 11 月 21 日
Depends. What do you want to do for the leftovers? How will this be used downstream in your code?
採用された回答
Adam Danz
2019 年 11 月 21 日
編集済み: Adam Danz
2019 年 11 月 21 日
Here's how to partition your row or column vector of data into a matrix with 365 rows and m columns. The last column may contain trailing NaN values as fillers.
% Create data
data = rand(1,19361);
n = 365; %number of elements per group
% add NaNs to the end of your data to
% create enough samples to split evenly
% into n-sample groups.
extraNeeded = n - mod(numel(data),n);
% Concatenate vertically
% if data is a row vector it will be changed to a column vector
data = [data(:); nan(extraNeeded,1)];
% partition your data
dataSections = reshape(data,n,[]);
Result
size(dataSections)
ans =
365 54
Group j can be accessed by
dataSections(:,j)
2 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!