How to split a number into 5 parts equalling the sum of the number
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Jon
2022-3-10
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
2022-3-11
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!