필터 지우기
필터 지우기

Chose vectors (from a randomly generated vectors) which does not contain more than two identical elements

조회 수: 1 (최근 30일)
I am generating a random vector of size 1x50 (using randi with range 1 to 400),several times(lets say 50000 times). But from these random vectors, I will chose only those vectors which does not contain more than two identical elements i.e. a vector having more than two identical elements will be thrown and rest will be saved in row wise matrix form. How to do this generally so that I can use the code for other cases(e.g. not more than four identical elements instead of two etc.)
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 5월 9일
I am not clear as to what exactly "not more than two identical elements" means, especially as you expand the number.
[1 1 1 1] -- contains 1 identical number? Contains 4 identical numbers?
[1 2 1 2] -- contains 2 identical numbers? Contains 4 identical numbers?
[1 2 3 1] -- contains 1 identical number? Contains 2 identical numbers?

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

채택된 답변

Guillaume
Guillaume 2017년 5월 9일
If I understood correctly, all you have to do is build the histogram of each row and if you more than two values greater than 1 per row you know that you have more than two duplicate elements. The deprecated histc works better for this than the newer histcounts since the former can operate on columns of a matrix (whereas the latter just flattens the matrix):
m = randi(400, 50000, 50); %50000 rows of 50 columns of random values between 1 and 400
mdist = histc(m.', 1:400); %transpose since histc works on columns
toomanydups = sum(mdist > 1) > 2; %more than 2 values whose distribution is greater than 1 ?
%note that toomanydups is a row vector that has as many columns as the rows of m
m(toomanydups, :) = []; %remove the rows with dups.
  댓글 수: 3
Guillaume
Guillaume 2017년 5월 9일
It's the exact same principle, except that you can also use histcounts instead of histc and you don't need to transpose the matrix, since there's only one row, so
for t = 1:50000
m = randi(400, 1, 50);
mdist = histc(m, 1:400);
%or mdist = histcounts(m, 'BinMethod', 'integers')
if sum(mdist > 1) > 2
%more than 2 duplicates
else
%less than 2 duplicates
end
end
Kamal Bera
Kamal Bera 2017년 5월 9일
Thanks. This is OK. But for my requirement, I need little adjustment as:
max(mdist)>2 instead of sum(mdist>1)>2

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

추가 답변 (1개)

Jan
Jan 2017년 5월 9일
편집: Jan 2017년 5월 9일
v = [randperm(400, 25), randperm(400, 25)];
v = v(randperm(50, 50));
Now v contains 50 random values from 1 to 400 and no value appears more than 2 times.
v = randi([1, 400], 50);
[B, N] = RunLength(sort(v));
if any(N > 2)
% More than 2 occurrences of a certain value
...
Another alternative (not tested):
P = ones(1, 50);
while ...
v = randi([1, 400], 50);
S = splitapply(@sum, P, v)
if any(S > 2)
...
I assume the constructive randperm method is the fastest.
  댓글 수: 1
Kamal Bera
Kamal Bera 2017년 5월 9일
Hi Simon, can you generalize your answer using randperm. I mean cases for : no value appears more than 1 times; no value appears more than 3 times; no value appears more than 4 times etc. Your code
v = [randperm(400, 25), randperm(400, 25)];
v = v(randperm(50, 50));
is applicable for-no value appears more than 2 times.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by