Finding All Positions For Multiple Elements in a Vector

Finding All Positions for Multiple Elements in a Vector

This is one way to do it. First I get the indices at which x is either 8 or 9. Then we can verify that at those indices, x is indeed 8 and 9.

> inds <- which(x %in% c(8,9))
> inds
[1] 1 3 4 12 15 19
> x[inds]
[1] 8 9 9 8 9 8

How to select multiple elements from multiple vectors in a list

We could use lapply

lapply(mylist[1:2], `[`, c(1, 3))

#[[1]]
#[1] -0.5604756 1.5587083

#[[2]]
#[1] 6 55

which is similar to map in purrr

purrr::map(mylist[1:2], `[`, c(1, 3))

To update the values of selected elements, we can do

mylist[1:2] <-lapply(mylist[1:2], function(x) {x[c(1, 3)] <- 0;x})
mylist
#[[1]]
#[1] 0.00000000 -0.23017749 0.00000000 0.07050839 0.12928774

#[[2]]
#[1] 0 61 0 8 8

#[[3]]
#[1] 4 3 8 7 6

data

set.seed(123)
mylist<- list(rnorm(5), rgeom(5, 0.05), rbinom(5, 10, 0.5))

How to find indices of element in one vector in other vector R

a <-  c('Q1', 'Q2', 'Q3')
b <- c('Q10', 'Q13', 'Q1', 'Q1', 'Q40', 'Q2', 'Q2', 'Q2')

which(b %in% a)

[1] 3 4 6 7 8

How to find multiple elements in an std::vector

For each wsBuf, you only call find once. This clearly finds ... one element.

it = std::find(it, wsReadFile.end(), wsBuf[i]);
if(it != wsReadFile.end())
{
DWORD index = std::distance(wsReadFile.begin(), it);
if(index < wsReadFile.size())
{
wsWriteFile.push_back(wsReadFile[index];
wsWriteFile.push_back(wsReadFile[index + 1];
wsWriteFile.push_back(wsReadFile[index + 2];
}
}

It also doesn't compile, you are missing some )s. In the future, post code that compiles and demonstrates the problem; if you don't know the problem, you cannot know what doesn't matter to solving it.

In any case, you need a loop. I'd also want bounds checking. And less iterator<->index conversion. And fewer types that don't matter.

auto it = std::find(wsReadFile.begin(), wsReadFile.end(), wsBuf[i]);
while(it != wsReadFile.end())
{
auto left = std::distance(it, wsReadFile.end());
// don't copy past the end:
auto copy_end = it+(std::min)(left, decltype(left)(3));
std::copy( it, copy_end, std::back_inserter(wsWriteFile) );
// find next:
it = std::find( copy_end, wsReadFile.end(), wsBuf[i]);
}

Find all positions of all matches of one vector of values in second vector

This should work:

which(hay %in% needles) # 2 3 5

Finding multiple max elements in a vector C++

You could modify your approach to keep a vector of indices where the maximum occurred:

#include <cfloat>
#include <iostream>
#include <utility>
#include <vector>

std::pair<double, std::vector<std::size_t>> FindMaxElements(std::vector<double> const& v)
{
std::vector<std::size_t> indices;
double current_max = -DBL_MAX;

for (std::size_t i = 0; i < v.size(); ++i)
{
if (v[i] > current_max)
{
current_max = v[i];
indices.clear();
}

if (v[i] == current_max)
{
indices.push_back(i);
}
}

return std::make_pair(current_max, indices);
}

int main()
{
auto result = FindMaxElements({1, 4, 7, 2, 7, 3});
std::cout << "max: " << result.first << '\n';
std::cout << "indices: ";
for (auto i : result.second)
std::cout << i << ' ';
}

Output

max: 7
indices: 2 4

Inserting multiple elements at same position in a vector

You can use nested vector to insert more than one values at a position.

Declaration vector< vector<int> > adj[V];

Now to insert a value at position 0 you can use like this

void addEdge(vector<int> adj[], int u, int v, int val) 
{
adj[u][v].push_back(val);
adj[v][u].push_back(val);
}

To add element

addEdge(adj, 0, 0, 1); // insert 1 at position (0,0)

Please keep in mind that before adding element you need to initialize vector at every index.

But you can't insert two or more values at same position in vector.

Position of elements from one vector in another vector with R

You are looking for pmatch:

a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,3,4,5)
pmatch(b,a)
#[1] 1 3 5 7 9

a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,5)
pmatch(b,a)
#[1] 1 3 4 5 7 9

a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,4,5)
pmatch(b,a)
#[1] 1 3 4 5 7 8 9

a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,4,5,5)
pmatch(b,a)
#[1] 1 3 4 5 7 8 9 10

a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,1,2,2,3,4,4,5,5)
pmatch(b,a)
#[1] 1 2 3 4 5 7 8 9 10

a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,1,2,2,3,3,4,4,5,5)
pmatch(b,a)
# [1] 1 2 3 4 5 6 7 8 9 10


Related Topics



Leave a reply



Submit