how to use "randi" in specific case?

4 views (last 30 days)
Majid
Majid on 30 Nov 2022
Commented: Voss on 30 Nov 2022
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.
  4 Comments
Torsten
Torsten on 30 Nov 2022
If your binary matrix had, e.g. (five 1's in some column) or (four 1's in some column and at least one 1 in the preceeding or next column), you couldn't do what you try to do. And then ?
Majid
Majid on 30 Nov 2022
@Torsten i want just a loop while, if the value exist in the column for more than one time or in just the previous or next column i will do another "randi"

Sign in to comment.

Accepted Answer

Voss
Voss on 30 Nov 2022
Maybe something like this (the example A requires at least 5 values, e.g., [10 15 20 25 30]):
A = [ ...
0 0 1 0 0 0 0 0 0 0; ...
0 0 1 0 0 0 0 0 0 0; ...
0 0 0 1 0 0 0 0 0 0; ...
0 0 0 1 0 0 0 0 0 0; ...
0 0 0 1 0 0 0 0 0 0; ...
0 0 0 0 1 0 0 0 0 0; ...
0 0 0 0 0 0 1 0 0 0; ...
0 0 0 0 0 0 0 1 0 0; ...
0 0 0 0 0 0 0 0 1 0; ...
0 0 0 0 0 0 0 0 1 0; ...
0 0 0 0 0 0 0 0 0 1; ...
0 0 0 0 0 0 0 0 0 1];
vals = [10 15 20 25 30];
[m,n] = size(A);
n_vals = numel(vals);
is_used = false(1,n_vals);
is_used_previous = false(1,n_vals);
for jj = 1:n
idx = find(A(:,jj));
for ii = 1:numel(idx)
if all(is_used | is_used_previous)
continue
end
unused_val_idx = find(~is_used & ~is_used_previous);
new_val_idx = randi(numel(unused_val_idx));
A(idx(ii),jj) = vals(unused_val_idx(new_val_idx));
is_used(unused_val_idx(new_val_idx)) = true;
end
is_used_previous = is_used;
is_used(:) = false;
end
disp(A);
0 0 15 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 20 0 0 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0 0 0 0 10 0 0 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 0 10 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 25 0 0 0 0 0 0 0 0 0 30

More Answers (1)

David Hill
David Hill on 30 Nov 2022
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
m = 4×10
0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 15 0 0
  7 Comments
Voss
Voss on 30 Nov 2022
@Majid: 25 exists in columns 3 and 4. Is that allowed?
Majid
Majid on 30 Nov 2022
@Voss no, because it is a e.g i just focus in one value"10", here just i take l=[10 15 20 25]; for my real case the range of values is large.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!