how to use "randi" in specific case?

조회 수: 4 (최근 30일)
Majid
Majid 2022년 11월 30일
댓글: Voss 2022년 11월 30일
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
Torsten
Torsten 2022년 11월 30일
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 2022년 11월 30일
@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"

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 11월 30일
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
  댓글 수: 2
Majid
Majid 2022년 11월 30일
@Voss it works, thank you very much!
Voss
Voss 2022년 11월 30일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
David Hill 2022년 11월 30일
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
Voss
Voss 2022년 11월 30일
@Majid: 25 exists in columns 3 and 4. Is that allowed?
Majid
Majid 2022년 11월 30일
@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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by