random matrix with fix summation
1 次查看(过去 30 天)
显示 更早的评论
Hello
please.
this is my simple code that create (3*3) random matrix that all elements are 0 or 1.
a=randi([0,1],[500,500])
but is there any way that I define the number of elements that are 1. for example number of elements that are 1 be 7000 or in other word summation of matrix "a" be 7000
Thank you.
0 个评论
采纳的回答
更多回答(1 个)
Adam
2015-1-15
编辑:Adam
2015-1-15
You can just create 7000 random indices instead and place the 1s at those indices
e.g. if you have a [500,500] matrix you have 250000 indices so create 7000 random indices from 1 to 250000 and then just assign 1s to all those indices of a 500 x 500 matrix.
2 个评论
Mohammad Abouali
2015-1-15
just change "... so create 70 random indices ..." to "... so create 7000 random indices ..."
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!