Chose vectors (from a randomly generated vectors) which does not contain more than two identical elements
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Kamal Bera
am 9 Mai 2017
Kommentiert: Kamal Bera
am 9 Mai 2017
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 Kommentar
Walter Roberson
am 9 Mai 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?
Akzeptierte Antwort
Guillaume
am 9 Mai 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 Kommentare
Guillaume
am 9 Mai 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
Weitere Antworten (1)
Jan
am 9 Mai 2017
Bearbeitet: Jan
am 9 Mai 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 Kommentar
Siehe auch
Kategorien
Mehr zu Numbers and Precision finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!