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 »

2017-08-14 15:08 (1) Answers

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 »

2017-08-14 12:08 (2) Answers

Name the header from a file

this is the script I'm on: data <- read.table(file.choose(), header = FALSE, sep = "") data.header <- read.table(file.choose(), header = FALSE, sep = "") data.names <- as.vector(data.header[1,]) names(data) <- data.names Basically, my...
more »

2017-08-11 23:08 (1) Answers

Haskell Pattern Match Vectors

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 »

2017-08-11 01:08 (1) Answers

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 »

2017-08-08 08:08 (2) Answers

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 »

2017-08-02 13:08 (1) Answers

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 »

2017-07-25 20:07 (5) Answers

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 »

2017-07-23 08:07 (3) Answers

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 »

2017-07-17 15:07 (3) Answers

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 »

2017-07-13 01:07 (1) Answers

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 »

2017-07-10 18:07 (2) Answers

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 »

2017-07-06 16:07 (1) Answers

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 »

2017-06-30 23:06 (2) Answers

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 »

2017-06-28 17:06 (1) Answers

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 »

2017-06-24 11:06 (1) Answers

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 »

2017-06-23 20:06 (1) Answers