Jacob
Jacob

Reputation: 29

Matching and eliminating elements of a vector of doubles

I have an input vector like:

[1.3, 2.2, 2.3, 4.2, 5.1, 3.2, 5.3, 3.3, 2.1, 1.1, 5.2, 3.1]

I would then like to check whether x.1 and x.2 and x.3 and x.y is in the vector and then discard if there isn't at least 3 y values with matching x values exist. So the example vector would look like:

[2.2, 2.3, 5.1, 3.2, 5.3, 3.3, 2.1, 5.2, 3.1] 

(1.3, 1.1, and 4.2 are removed due to only 2 and 1 x value). It has to work for a vector of any length. I just started to try learn Matlab from a guide but I simply cant complete this question :(

Upvotes: 0

Views: 51

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112689

You can

  1. Compute the integer part of x;
  2. Count how may times each obtained integer part occurs, and to each value of x associate its corresponding number of times;
  3. Keep only the values of x whose integer part occurs at least n=3 times.

Code:

x = [1.3 2.2 2.3 4.2 5.1 3.2 5.3 3.3 2.1 1.1 5.2 3.1]; %// data
n = 3;                                                 %// min required number
xf = floor(x);                                         %// step 1
[ii, jj] = histc(xf, unique(xf));                      %// step 2
result = x(ismember(jj, find(ii>=n)));                 %// step 3

Upvotes: 2

Related Questions