朱莉娅(Julia)中有固定行总和的整数的随机矩阵
我想创建一个带有以下约束的随机矩阵:
- 所有值均应是整数值
- 固定行总和(用第100、50、10、5等行减小)
- 随机值从0到6,
我尝试创建一个随机矩阵并将其扩展到我想要的固定行总和,但是后来我得到了我获得浮点值的问题(圆形也无济于事)
A = rand(0:6, 5, 10)
sum_vec = [10,5,3,2,2]
for i = 1:length(A[:,1])
# row sum
s = sum(A[i,:])
A[i,:] = (A[i,:] /s ) * sum_vec[i]
end
非常感谢您的帮助!
I would like to create a random matrix with the following constraints:
- all values should be Integer values
- fixed row sums (decreasing with row e.g. 100, 50, 10, 5 etc.)
- random values from 0 to 6
I tried to create a random matrix and scale it to my fixed row sums I want, but then I get the problem that I get Float values (rounding does not help either)
A = rand(0:6, 5, 10)
sum_vec = [10,5,3,2,2]
for i = 1:length(A[:,1])
# row sum
s = sum(A[i,:])
A[i,:] = (A[i,:] /s ) * sum_vec[i]
end
Thanks a lot for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你还没有说你的随机数应该遵循什么分布,
所以我做出了一些武断的选择。
这是一个示例函数,您可以使用它来生成您想要的内容,其中
n
是必须是 0 到 6 之间的整数的元素数量,s
是它们所需的和:You have not said what distribution your random numbers should follow,
so I have made some arbitrary choice.
Here is an example function you can use to generate what you want, where
n
is the number of elements that have to be integers from 0 to 6, ands
is their desired sum: