how to use "randi" in specific case?
Show older comments
Hi!
i want to ask you if it is possible to use "randi" in this case:
i have a binary matrix A (m*n), each row has just "1" and other elements are set to "0", i want to put randomly in the "1" position a value from this range for example [10,15,20,25].
i'm asking how can i remove the case of seeing the same value in the same column or two same values in two successive columns.
for example:
0 0 10 0 0 0 0 0 0 0
0 0 10 0 0 0 0 0 0 0
0 0 0 15 0 0 0 0 0 0
0 0 0 20 0 0 0 0 0 0
0 0 0 10 0 0 0 0 0 0
0 0 0 0 25 0 0 0 0 0
0 0 0 0 0 0 20 0 0 0
0 0 0 0 0 0 0 15 0 0
0 0 0 0 0 0 0 0 10 0
0 0 0 0 0 0 0 0 10 0
0 0 0 0 0 0 0 0 0 15
0 0 0 0 0 0 0 0 0 25
as you see the third column i have the same value "10" ,also in the fourth i have the same value of previous column, i want to remove this case.
i'll appreciate any help, thank you very much.
Accepted Answer
More Answers (1)
n=10;%length of matrix
l=[10 15 20 25];
m=zeros(length(l),n);
r1=randperm(length(l));r2=randperm(n,length(l));r3=randperm(length(l));
for k=1:length(l)
m(r1(k),r2(k))=l(r3(k));
end
m
7 Comments
Majid
on 30 Nov 2022
m=20;n=10;
A=zeros(m,n);
for k=1:m
A(k,randi(n))=1;
end
l=[10 15 20 25];
A(A==1)=l(randi(length(l),1,nnz(A)))
h=histc(A,l);
[r,c]=find(h>1);
[R,C]=find(movsum(h>=1,2,2,'endpoints','discard')>1);
C=C+1;
idx=[];
for k=1:length(r)
f=find(A(:,c(k))==l(r(k)));
idx=[idx;f(2:end)];
end
for k=1:length(R)
idx=[idx;find(A(:,C(k))==l(R(k)))];
end
idx=unique(idx);
A(idx,:)=[]
Majid
on 30 Nov 2022
David Hill
on 30 Nov 2022
You keep changing the problem. What are you trying to do? What is the big picture? I think you can figure it out from here.
Majid
on 30 Nov 2022
Majid
on 30 Nov 2022
Categories
Find more on Univariate Discrete Distributions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!