Finding the Index of First Changes in the Elements of a Vector

Finding the index of first changes in the elements of a vector

rle is a good idea, but if you only want the indices of the changepoints you can just do:

c(1,1+which(diff(v)!=0))
## 1 8 10

Is there an R function for finding the index of an element in a vector?

The function match works on vectors:

x <- sample(1:10)
x
# [1] 4 5 9 3 8 1 6 10 7 2
match(c(4,8),x)
# [1] 1 5

match only returns the first encounter of a match, as you requested. It returns the position in the second argument of the values in the first argument.

For multiple matching, %in% is the way to go:

x <- sample(1:4,10,replace=TRUE)
x
# [1] 3 4 3 3 2 3 1 1 2 2
which(x %in% c(2,4))
# [1] 2 5 9 10

%in% returns a logical vector as long as the first argument, with a TRUE if that value can be found in the second argument and a FALSE otherwise.

Find index of change in a column

You could try to compare shifted vectors, e.g.

which(x[-1] != x[-length(x)])
## [1] 3 5 6

This will work both on characters and factors

Find index where elements change value numpy

You can get this functionality in numpy by comparing each element with it's neighbor;

v[:-1] != v[1:]

array([False, False, False, False, True, False, False, True, True,
True, True, True, True, True, True, True, False, False], dtype=bool)

to get the indices you use the "where" function

np.where(v[:-1] != v[1:])[0]

array([ 4, 7, 8, 9, 10, 11, 12, 13, 14, 15])

From here you can prepend the first element and add a one to get to the same indexing scheme you have in your question.

Find index of vector element using search_n function

The std::search_n function looks for a sequence of a specified number of occurrences of a particular value in a range; that number is the third argument (count on this cppreference page).

So, if you insist on using std::search_n for this, you will need to add an extra argument (count, which will be 1) in your call:

it = std::search_n(a.begin(), a.begin() + n, 1, number);

However, using search_n is something of an overkill when looking for a single value; better to use the simpler (and faster) std::find function. Also, in place of a.begin() + n, you can use the easier and clearer a.end().

it = std::find(a.begin(), a.end(), number);

Also note that indexes and iterator positions in C++ start at zero, so, with the above fix(es) applied to your code, the answer will be "found at position 4"; if you want a 1-based position, then add 1 to the position; something like this:

auto position = std::distance(a.begin(), it) + 1;

How to return the index where values change in a c++ vector?

There are a few ways to solve this, but I believe this should work:

std::vector<std::vector<int>> vec2D;
int index = -1;
// Identify the indices of the position vector and use that to identify the
// correct indices of the vec.
for (int i = 0; i != position.size(); ++i)
{
// if the value at i of the position vector is 0,
// push_back the value at i of the vec vector into
// the correct vector of vector.
if (0 == position[i])
{
vec2D[index].push_back(vec[i])
}
else if (1 == position[i])
{
++index; //increment to the next vector
std::vector<int> temp;
vec2D.push_back(temp);
vec2D[index].push_back(vec[i])
}
}

How to find the index of the value where vector elements converge?

According to your edit, assume that the vector will always converge and the converged value is the last element (A(end)). Also, assume that when converged, the values are equal to the last element.

The idea is to first find the index of the last element that is not equal to the last element. Then, the index + 1 is the index of the first converged element, i.e. find(A~=A(end),1,'last') + 1

Example 1:

A = [1.444, 1.425, 1.435, 1.438, 1.438, 1.436, 1.436, 1.436, 1.436, 1.436];
index = find(A~=A(end),1,'last') + 1

Output:

index =

6

Example 2

B = [1 2 1 4 2 5 6 2 5 5 5 5 5 5 5 5 5 5 5];
index = find(B~=B(end),1,'last') + 1

Output:

index =

9

Example 3

C = [224.424 224.455 224.454 224.456 224.456 224.452 224.451 224.456 224.454 224.454 224.454 224.454 224.454 224.454 224.454];
index = find(C~=C(end),1,'last') + 1

Output:

index =

9

Update:

Since you are dealing with convergence, it is better to specify the tolerance for convergence. For example:

tolerance = 1e-5;

A = [1.444, 1.425, 1.435, 1.438, 1.438, 1.436, 1.436, 1.436, 1.436, 1.436];
index = find(abs(A - A(end)) >= tolerance,1,'last') + 1

Output:

index =

6

How to find all indexes of vectorvectorint for the minimum first index

You may do the job in linear time:

  • first find minimum
  • then iterate on minima.

Something like:

auto cmp = [](const auto& lhs, const auto& rhs) { return lhs[0] < rhs[0]; }
auto minIt = std::min_element(mySol.begin(), mySol.end(), cmp);
auto eqToMin = [&](const auto& value) { return (*minIt)[0] == value[0]; }

for (auto it = minIt; it != mySol.end(); it = std::find_if(it + 1, mySol.end(), eqToMin)) {
std::cout << (*it)[1] << std::endl;
}

find the place where the variable in a dataframe changes its value

Here is a possible way. Use diff to get the values where column b changes but be carefull, the first value of b, by definition of change, hasn't changed. (The problem is that diff returns a vector with one less element.)

inx <- c(FALSE, diff(data$b) != 0)
data[inx, ]
# a b
#4 4 1

After seeing the OP's comment to another post, the following code shows that this method can also solve the issue when b starts with any value,not just zero.

data2 <- data.frame(a=c(1,2,3,4,5,6),b=c(1,1,1,0,0,0))
inx <- c(FALSE, diff(data2$b) != 0)
data2[inx, ]
# a b
#4 4 0


Related Topics



Leave a reply



Submit