vectorization

Is there a better (i.e vectorised) way to put part of a column name into a row of a data frame in R

I have a data frame in R that has come about from running some stats on the result of a melt/cast operation. I want to add a row into this dataframe containing a Nominal value. That Nominal Value is present in the names for each column df<-as.data.frame(cbind(x=c(1,2,3,4,5),`Var A_100`=c(5,4,3,2,1),`Var B_5`=c(9,8,7,6,5))) > df x Va...

vectorizing a for loop in numpy/scipy?

I'm trying to vectorize a for loop that I have inside of a class method. The for loop has the following form: it iterates through a bunch of points and depending on whether a certain variable (called "self.condition_met" below) is true, calls a pair of functions on the point, and adds the result to a list. Each point here is an element i...

MATLAB: vectorized array creation from a list of start/end indices

I have a two-column matrix M that contains the start/end indices of a bunch of intervals: startInd EndInd 1 3 6 10 12 12 15 16 How can I generate a vector of all the interval indices: v = [1 2 3 6 7 8 9 10 12 15 16]; I'm doing the above using loops, but I'm wondering if there's a more elegant vec...

auto vectorization on double and ffast-math

Why is mandatory to use -ffast-math with g++ to achieve vectorization of loop using doubles? I don't like ffast-math because I don't want to loose precision. ...

Vectorizing sums of different diagonals in a matrix

I want to vectorize the following MATLAB code. I think it must be simple but I'm finding it confusing nevertheless. r = some constant less than m or n [m,n] = size(C); S = zeros(m-r,n-r); for i=1:m-r+1 for j=1:n-r+1 S(i,j) = sum(diag(C(i:i+r-1,j:j+r-1))); end end The code calculates a table of scores, S, for a dynamic...

Parallelize or vectorize all-against-all operation on a large number of matrices?

I have approximately 5,000 matrices with the same number of rows and varying numbers of columns (20 x ~200). Each of these matrices must be compared against every other in a dynamic programming algorithm. In this question, I asked how to perform the comparison quickly and was given an excellent answer involving a 2D convolution. Seria...

Do any JS implementations currently support (or have support on the roadmap for) fast, vectorized operations on Arrays or similar?

I'd like to do a bit of matrix/vector arithmetic in JavaScript, and was wondering if any browsers or other JS implementations actually have support for vectorized operations, for instance for quickly summing the entries of two Arrays (or summing, or whatever). Even if that currently doesn't mean it compiles down to vectorized operations...

R strsplit and vectorization

When creating functions that use strsplit, vector inputs do not behave as desired, and sapply needs to be used. This is due to the list output that strsplit produces. Is there a way to vectorize the process - that is, the function produces the correct element in the list for each of the elements of the input? For example, to count the l...

Server-side conversion of raster images to vector images

I would like to convert images that have been uploaded by the user (in various formats and conditions) to a vector image format such as .eps. I'm primarily working in PHP. What options exist? ...

Return value from column indicated in same row.

Hi, I'm stuck with a simple loop that takes more than an hour to run, and need help to speed it up. Basically, I have a matrix with 31 columns and 400 000 rows. The first 30 columns have values, and the 31st column has a column-number. I need to, per row, retrieve the value in the column indicated by the 31st column. Example row: [26...

[R] how to avoid loops

HI All, I'm new to R. I have two panel data files, with columns "id", "date" and "ret" file A has a lot more data than file B, but i'm primarily working with file B data. Combination of "id" and "date" is unqiue indentifier. Is there an elegent way of looking up for each (id, date) in B, I need to get the past 10 days ret from file...

MATLAB: comparison of cell arrays of string

I have two cell arrays of strings, and I want to check if they contain the same strings (they do not have to be in the same order, nor do we know if they are of the same lengths). For example: a = {'2' '4' '1' '3'}; b = {'1' '2' '4' '3'}; or a = {'2' '4' '1' '3' '5'}; b = {'1' '2' '4' '3'}; First I thought of strcmp but it would r...

Can this piece of matlab script be vectorized further?

So what I am trying to do with this code is find all pixels on a line of an image that are below a certain threshold. The problem, however, is that this code is executed in a double for loop (yeah I know :( ), once for each pixel, so it's very slow. I'm wondering if there's anything else I can do. Some tips would be great, as I'm pretty...

MATLAB: finding islands of zeros in a sequence

Imagine you have a very long sequence. What is the most efficient way of finding the intervals where the sequence is all zeros (or more precisely the sequence drops to near-zero values abs(X)<eps): For simplicity, lets assume the following sequence: sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0]; I'm trying to ge...

Compiler errors while building a project which uses Eigen, the C++ template library for linear algebra...

Hi Guys, in my project I'm making use of Eigen C++ library for linear algebra and ONLY when I turn on the vectorization flags (mfpu=neon -mfloat-abi=softfp) for ARM NEON, I get compiler errors. I'm not able to understand whats going wrong. Do I need to enable any preprocessor directives for ARM NEON in the Eigen Library? main.c #inc...

Optimizing the Verhoeff Algorithm in R

I have written the following function to calculate a check digit in R. verhoeffCheck <- function(x) { ## calculates check digit based on Verhoeff algorithm ## note that due to the way strsplit works, to call for vector x, use sapply(x,verhoeffCheck) ## check for string since leading zeros with numbers will be lost if (class(x)!="charac...

Creation of a specific vector without loop or recursion in R

Hi guys! I've got a first vector, let's say x that consists only of 1's and -1's. Then, I have a second vector y that consists of 1's, -1's, and zeros. Now, I'd like to create a vector z that contains in index i a 1 if x[i] equals 1 and a 1 exists within the vector y between the n precedent elements (y[(i-n):i])... more formally: z <- ...

Apply function to all rows in matlab

I have a function, ranker, that takes a vector and assigns numerical ranks to it in ascending order. For example, ranker([5 1 3 600]) = [3 1 2 4] or ranker([42 300 42 42 1 42] = [3.5 6 3.5 3.5 1 3.5] . I am using a matrix, variable_data and I want to apply the ranker function to each row for all rows in variable data. This is my curren...

Compute set union of a cell array in Matlab

I have a cell array where each element consists of a vector of ids. I like to compute the union of all elements in the cell array. This is my current solution, but I feel it can be vectorized or have a more elegant solution: union_ids = union(encounter_ids{1},encounter_ids{2}); for i=3:1:numel(encounter_ids); union_ids = union(unio...

MATLAB: sorting rows of two matrices using same ordering

Possible Duplicate: Sort a matrix with another matrix Given two matrices A and B of the same size, I want to sort A across the second dimension (rows) and apply the same ordering to the matrix B. Is it possible to vectorize this current code? r = 10; c = 4; A = rand(r,c); B = reshape(1:r*c,c,r)'; % can be any random matrix' ...