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

1 visualizzazione (ultimi 30 giorni)
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 Commento
Walter Roberson
Walter Roberson il 9 Mag 2017
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?

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 9 Mag 2017
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 Commenti
Guillaume
Guillaume il 9 Mag 2017
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 il 9 Mag 2017
Thanks. This is OK. But for my requirement, I need little adjustment as:
max(mdist)>2 instead of sum(mdist>1)>2

Accedi per commentare.

Più risposte (1)

Jan
Jan il 9 Mag 2017
Modificato: Jan il 9 Mag 2017
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 Commento
Kamal Bera
Kamal Bera il 9 Mag 2017
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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by