How to create a 3 by 3 matrix with the values from zero to 1 randomly but the there should be at least three 1 in the matrix
13 次查看(过去 30 天)
显示 更早的评论
i was going to make a 3 by 3 matrix which randomly generates values from 0 to 1 but the catch was to have at least three 1 in the matrix. pleeeeasee kindly help me :(
1 个评论
回答(1 个)
Jan
2015-9-30
A = rand(3, 3);
A(randperm(9, 3)) = 1;
3 个评论
Jos (10584)
2015-9-30
I never noticed, but there seems to be an inconsistency in the documentation of RAND with respect to open and close intervals:
R = rand(N) returns ... on the open interval(0,1).
while one of the examples suggests a closed interval:
Example 1: Generate values from the uniform distribution on the
interval [a, b].
r = a + (b-a).*rand(100,1);
(ML 2014a)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!