How to split a number into 5 parts equalling the sum of the number
11 views (last 30 days)
Show older comments
So i am trying to create a code that reads a large CSV that:
- counts the rows
- divides the rows by 5
- create an array that stores these parts [1 1part 2part 3part 4part 5part]
- create a new csv's.parts(1-5)
So read a csv and split it into 5 parts.
The idea i had was splitNum = rowcount/5 and then splitArray = [1:rowcount:totalrows] and then loop the table with this array until done
but ofcourse this doesnt equal the total number of rows properly.
What would be a better solution for this?
Thanks
0 Comments
Answers (2)
Jon
on 10 Mar 2022
Not sure exactly what you are expecting, but obviously if your total number of rows is not a multiple of 5 then you cannot split your original file into 5 files with equal number of rows.
If you want you could have all but the last file have the same number of rows using
n = floor(totalrows/5)
split = 1:n:totalrows
So for example if totalrows = 23, then you would have 5 files, each with 4 rows and then the last one would have only 3.
Walter Roberson
on 11 Mar 2022
CSV = readmatrix(filename) ;
R = size(CSV, 1);
N = floor(R/5);
parts = mat2cell(CSV, [N, N, N, N, R-4*N],size(CSV, 2));
Now write out parts{1} to 5
The last part might be up to 4 samples larger.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!