How to divide an array

조회 수: 2 (최근 30일)
Jaydeep Kansara
Jaydeep Kansara 2019년 11월 21일
댓글: Adam Danz 2019년 11월 21일
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
James Tursa 2019년 11월 21일
Depends. What do you want to do for the leftovers? How will this be used downstream in your code?
Jaydeep Kansara
Jaydeep Kansara 2019년 11월 21일
Thanks for the reply. I want to keep the leftovers and store them. there are 53 full array and leftocvers should be stored in 54th array.

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

채택된 답변

Adam Danz
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
Jaydeep Kansara
Jaydeep Kansara 2019년 11월 21일
Thanks Adam.
Adam Danz
Adam Danz 2019년 11월 21일
Glad I could help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by