Reputation: 5
how to do this?
i want to place a specific number, like number 2 randomly into a 3x3
of zeros array
,
it did came out, but the coding still not right and somehow number 1 also appear.
rows = 3;
cols = 3;
M = zeros(rows,cols);
p = randi(2);
rV = randperm(rows);
cV = randperm(cols);
M(rV(2),cV(2))=p
where did i do wrong? any suggestion
Upvotes: 0
Views: 25
Reputation: 5
No need - I've already solved it!
a=[2];
m=zeros(3);
m(randperm(numel(m),numel(a)))=a
Upvotes: 0
Reputation: 25140
You could also use randi
together with linear indexing
m = zeros(3);
m(randi(numel(m))) = 2;
Upvotes: 1