## Recycle a vector of characters in negative and positive direction

lets say I have vector of characters, letters and symbols vec<-c(letters, 0:9, LETTERS, c("!","ยง","\$","%","&")) vec I would like to build a function recycle that can recycle the vector vec so that recycle(vec, 68) would be similar to vec[6...
more »

## C++ vector storing generic T type classes

I have a Foo class that expects T type values. I'd also like to store them in a vector. I've tried several methodologies, but didn't find any solutions. Please have a look at the source code below to get the idea what I'd like to achieve. #include &...
more »

## Run function on vector without sapply

I would like to understand how I can write a function to execute a certain rule on the elements of a vector. Assume I want to multiply by a value all the elements of a vector, the below code returns: a<-c(1, 3, 5, 7, 10) f <-function(x){ f<...
more »

more »

## Trying to pass more than 2 vectors to a function in R

I want to compute e^(ax+b) over a=-1:1 and b=-1:1 for various values of X. I want the output in the form a list of 5 elements. Each element in the list is 3X3 Matrix. I did achieve this using Outer and Vectorize. sigm = function(a=0,b=0,x){ retu...
more »

I'm following an online tutorial on Haskell. We define a function to add two-dimensional vectors, represented by tuple pairs of numbers. The following is the explicit type declaration, that ensures both inputs are two-dimensional vectors. addVectors...
more »

## c++ - sorting a vector of custom structs based on frequency

I need to find the most frequent element in an array of custom structs. There is no custom ID to them just matching properties. I was thinking of sorting my vector by frequency but I have no clue how to do that. ...
more »

## C++ Vector of Monsters

I want to make a vector where the type is my overloading constructor or to be precise I want to make a vector of monsters but I cant seem to pass it and I have no idea why. What did I do wrong? // Monster.cpp #include <iostream> #include <...
more »

## c++ How to sort the rows of a 2d Vector, by the values in each rows column

The Aim: I have a 2d vector. I want to sort it by the value in its 4th column. For example I want to sort this vector: vector<vector<double>> vector1 = {{4,3,5,3}, {2,6,3,7}, ...
more »

## Sort a vector of wstrings, but case insensitive

When sorting a vector of wstrings with: vector<wstring> v; v.push_back(L"zzz"); v.push_back(L"BBB"); v.push_back(L"aaa"); sort(v.begin(), v.end()); the result is BBB, aaa, zzz. How to make a case insensitive sort instead? I tried wi...
more »

## Efficiently create derangement of a vector in R

I'm looking into a way of efficiently creating a derangement (and conversely specific permutations) of a vector in R. As far as I've seen, there's no base function that does that and also there's not much about it here on SO. An obvious start is sam...
more »

## Does sorting a vector of std::wstring actually rewrite the raw string data?

Let's sort a vector of 1 million of strings with: std::vector<std::wstring> v; // fill the vector std::sort(v.begin(), v.end()); Does it: rewrite the vector with new wstrings, containing a few bytes for the wstring structure its...
more »

## Vector that contains keys to sort a map by values

I have a: std::map<long, std::wstring> fNames; // ex: fNames[FileReferenceNumber] = L"hello.txt" As std::map has keys ordered, but not values (a hash-table unordered_map has even nothing ordered), I'd like to create a vector: std::vec...
more »

## Is it possible to skip NA values in "+" operator?

I want to calculate an equation in R. I don't want to use the function sum because it's returning 1 value. I want the full vector of values. x = 1:10 y = c(21:29,NA) x+y [1] 22 24 26 28 30 32 34 36 38 NA x = 1:10 y = c(21:30) x+y [1] 22 24 26 28...
more »

## Replace string in R with patterns and replacements both vectors

Let's say I have two vectors like so: a <- c("this", "is", "test") b <- c("that", "was", "boy") I also have a string variable like so: string <- "this is a story about a test" I want to replace values in string so that it becomes the f...
more »

## Vectors passed to function aren't being modified

So I am making a function to take certatin elements of a vector and storing them into another vector. Whenever I run the program, it gives a segmentation fault error? Why does this happen and how can I fix things so that this doesn't happen? #inclu...
more »

## Sympy: How to compute Lie derivative of matrix with respect to a vector field

I have a system(x'=f(x)+g(x)u), such that f(x) is f:R3->R3 and g(x) is g:R3->R(3x2). My system is As you can see, it is a MIMO nonlinear control system and I wish to find the controllability matrix for my system. Controllability matrix in ...
more »

## Why `a` and `b` don't generate the same set of numbers using `mapply` in R?

I'm wondering why a and b don't generate the same set of numbers in the following: N = 60 ; df = N-1 ; d = 3 ; ncp = d*sqrt(N) a = mapply(c, as.list(14: d*sqrt(N)+5), as.list(-14: -d*sqrt(N)+5), SIMPLIFY = FALSE) b = mapply(c, as.list(14: ncp...
more »

## How to generate a list of negative numbers paired with their positive mirrors?

I was wondering how I could generate a list of paired values that start from c(-30, 30), c(-29, 29), ..., c(-20, 20)? (Each pair has a negative number with its positive mirror). I tried something like the following with no success: c(-30:-20, 30:...
more »

## How to delete numbers from a vector?

I have this vector v = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,19,20) I want to remove the multiples of 2 and 3. How would I do this? I tried to do this but I doesn't work: import numpy as np V = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,1...
more »

## How does this one-hot vector conversion work?

When I was working on my machine learning project, I was looking for a line of code to turn my labels into one-hot vectors. I came across this nifty line of code from u/benanne on Reddit. np.eye(n_labels)[target_vector] For example, for a target_...
more »

## C++ pointers and references in functions

I am coming from a C#/Java background into C++, using visual studio community 2017 & plenty of tutorials. I came to the point where am unsure of what is a correct way to write a function to process a vector of data. Should I force a function to u...
more »

## How can I iteratively apply the function on a data in c++?

I'm just looking for a general pseudo-code type in which if I have a data type of vector<vector<double>> Xand it needs to iteratively go through a function myFunction(vector<vector<double>> &X) that returns the same data t...
more »

## Modify complicated character vector automatically

I have a complicated character vector, whereby each element of the vector consists of numbers and letters. I want to simplify this vector, so that sequences of numbers and/or letters are put into ranges. Here is an example, how the input and output v...
more »

## Unexpected error when using a reference of vector in a third pary function

I am supposed to input an array of values for x in the below third party function. bool MyNLP::get_starting_point(Index n, bool init_x, double* x, bool init_z, double* z_L, double* z_U, Index m, ...
more »

## Sorting a container multiple times, what container and what aproach to use

I have some data which I need to print, for simplicity lets say it is a container (vector) of people which have some parameters. In different parts of my program, I need to print all of them sorted by different parameter. My question are 1.) which c...
more »

## Sorting multiple vectors in single sort call C++

This answer demonstrates how to efficiently obtain an indices vector using std::sort on a vector of values using the nice new-ish C++11 functionality (there's also a variety of duplicates of that question as well). It also hints that you can obtain t...
more »

## Expand vector n times to fit larger matrix without using np.tile() or creating a new vector

I have matrix (shape=3,12) and want to do column-to-column multiplication with vector kkk (len=6). I can do it by making np.tile(kkk,2) to expand it as 12 element vector. But I wonder there is a way to just multiply it straightforwardly without using...
more »

## Plot data against time

I'm trying to plot vector a, which contains returns on a stock over nearly 15 years (about 5000 values in total), against vector b, my time vector. a = (0.1, 0.2, 0.1, 0.15, 0.5,...,0.2) b = ("2001-07-23",...,"2015-12-31") I transformed both vecto...
more »

## How to add two vectors of the same size in php

Lets say I have the following code: <?php \$a = array(0,1,2); \$b = array(0,1,2); \$c = \$a + \$b; print_r(\$c); ?> it does not work. Obviously, I can add each corresponding element of the vector by having a "foreach" loop, but I am wondering if t...
more »

## How to create a vector of class objects

I'm trying to create a vector that contains various class times. Afterwards, I would compare these times to see which one is earlier through a sorting function. Edit: after some people mentioned, I do wish to do this with an older version of C++ (pr...
more »

## C++ sort vector<double> vs vector<Object> keyed on double member variable

I have a vector of custom objects I'm sorting and noticed that the sort took a bit longer than expected. I decided to look further into it and adapted the code from this sort benchmark where instead of vector<double>, I'm sorting a vector<F...
more »

## How to create a database of feature vectors for images?

I have a database of around 4 classes each consistent of around 50 images. I created a features vector on Matlab to extract the features I need from each image. How to create the database of vectors for all the images to then train them per class? ...
more »

## Find the first set of consecutive integers in a vector

I have a dataset where I am trying to find the first instance in a consecutive set of rows that are identical. So let's say given this dataset: df <- data.frame(trial = c(1:16), DV = c(2, 3, 2, 3, 3, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2, 1)) If I were...
more »