matlab - 分箱 1:9 -> 等号元胞数组的分箱
问题描述
以下简单问题:我想将一个向量放入一个大小相等的单元格数组groups
中bins。
我在解决问题时遇到了麻烦,我有一种强烈的感觉可能是单线,这是我已经走了多远:
nums=1:9; %numbers to bin
categories=discretize(nums,3); %put nums in 3 equal groups
groups=mat2cell(x); % should return: {1:3,4:6,7:9}
我错过了什么?
该解决方案应该适用于任何包含数字的一维向量,尽可能将其分箱到相同大小的箱中(任何解决方案都有效);输出应该是各个 bin 的单元格数组。
解决方案
result = num2cell(reshape(1:9,3,[]),1);
如果数组大小不能被您可以使用的箱数整除,histcounts
并且mat2cell
:
nbins = 3;
a= [2 3 1 8 7 6 9 8 1];
result = mat2cell(a,1,histcounts(1:numel(a),nbins));
推荐阅读
- c# - 表行服务器端单击转发器?
- python - Django TemplateHTMLRenderer - 渲染 HTML 视图
- cucumber - Testng 没有在 Cucumber Hooks 中执行代码
- excel - Excel中的Google Api计算地点之间的行驶距离
- java - java年轻gc比用户花费更长的系统时间
- wpf - TextBlock 文本属性已绑定到源,我可以格式化文本显示吗?
- dart - TextSize 在颤动中不根据 deviceSize 或 devicePixelRatio 进行缩放
- android - Google 动态交付 INSTALL_FAILED_INVALID_APK
- jpa - 在 eclipselink jpa 中启用嵌套事务
- reinforcement-learning - 强化不受智能体影响的环境的学习