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

请先登录,再进行评论。

更多回答(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