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 个评论
WAT
WAT 2015-9-30
编辑:WAT 2015-9-30
This is kind of an odd question. What distribution are the numbers intended to have (the obvious guess is uniform, but I don't know if that can be assumed).
Is the number of "1" elements also supposed to be random? What about their locations?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2015-9-30
A = rand(3, 3);
A(randperm(9, 3)) = 1;
  3 个评论
Jos (10584)
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)
WAT
WAT 2015-9-30
编辑:WAT 2015-9-30
Unless the number of "1" elements is supposed to be at least 3 (but possibly as high as 9). If you assume this is the case and that the number is supposed to be uniformly from [3,9] then
A = rand(3, 3);
A(randperm(9, Randi([3,9]))) = 1;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by