Saving results from loop with intervals
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone, I am applying Naive Bayes Classification by hand to a data set with formula. My data has 45.000 columns and no class informations. I would like to seperate it with classes. I will have nearly 100 classes but I don't want to write it by hand. I want to handle it with loop and I am looking for hours but I couldn't figure it out.
veri = readtable("2018.csv");
maxgh = max(veri.GlobalHoriz_W_m_2_);
mingh = min(veri.GlobalHoriz_W_m_2_);
meangh = mean(veri.GlobalHoriz_W_m_2_);
stdgh = std(veri.GlobalHoriz_W_m_2_);
vericell = table2cell(veri);
ghcolumn = cell2mat(vericell(:,3));
*class1 = vericell(ghcolumn>=mingh & ghcolumn <=95,:);
class2 = vericell(ghcolumn>95 & ghcolumn <=195,:);
class3 = vericell(ghcolumn>195 & ghcolumn <=295,:);
class4 = vericell(ghcolumn>295 & ghcolumn <=395,:);
class5 = vericell(ghcolumn>395 & ghcolumn <=495,:);
class6 = vericell(ghcolumn>495 & ghcolumn <=595,:);
class7 = vericell(ghcolumn>595 & ghcolumn <=695,:);
class8 = vericell(ghcolumn>695 & ghcolumn <=795,:);
class9 = vericell(ghcolumn>795 & ghcolumn <=895,:);
class10 = vericell(ghcolumn>895 & ghcolumn <=995,:);
class11 = vericell(ghcolumn>995 & ghcolumn <=maxgh,:);*
I would like to create seperate tables for the values which is between -5,95 95,195 195,295 etc.. with loop of course
For now I increased the numbers for 100 but I would like to increase them by 10 and I don't want to do it one by one with hand . Any help will be appreciated so much. Thanks in advance.
4 个评论
回答(1 个)
Guillaume
2018-2-28
编辑:Guillaume
2018-2-28
As Stephen and I said, splitting your table into multiple tables is probably going to complicate things for you. I would recommend you create a new column instead which tells you which class each row belongs to:
veri = readtable("2018.csv");
veri.Class = discretize(veri{:, 3}, min(veri{:, 3}):20:max(veri{:, 3})+20)
That's all that is needed. No loop required. You can then do calculations by class using rowfun or varfun with the 'GroupingVariable', 'Class' option. For example, to get the mean of column 4 per each class:
varfun(@mean, veri, 'InputVariables', 4, 'GroupingVariables', 'Class')
If you really want to split the table:
splittables = splitapply(@(rows) {veri(rows, :)}, (1:height(veri))', veri.Class)
splittables{i} is the table of class i. But again, this should not be needed. Calculating the above mean is more complicated once it's split.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Events 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!