vectorization

Create a zero-filled 2D array with ones at positions indexed by a vector

二次信任 提交于 2019-12-17 04:08:32
问题 I'm trying to vectorize the following MATLAB operation: Given a column vector with indexes, I want a matrix with the same number of rows of the column and a fixed number of columns. The matrix is initialized with zeroes and contains ones in the locations specified by the indexes. Here is an example of the script I've already written: y = [1; 3; 2; 1; 3]; m = size(y, 1); % For loop yvec = zeros(m, 3); for i=1:m yvec(i, y(i)) = 1; end The desired result is: yvec = 1 0 0 0 0 1 0 1 0 1 0 0 0 0 1

Do any JVM's JIT compilers generate code that uses vectorized floating point instructions?

隐身守侯 提交于 2019-12-17 03:48:21
问题 Let's say the bottleneck of my Java program really is some tight loops to compute a bunch of vector dot products. Yes I've profiled, yes it's the bottleneck, yes it's significant, yes that's just how the algorithm is, yes I've run Proguard to optimize the byte code, etc. The work is, essentially, dot products. As in, I have two float[50] and I need to compute the sum of pairwise products. I know processor instruction sets exist to perform these kind of operations quickly and in bulk, like SSE

Vectorized IF statement in R?

耗尽温柔 提交于 2019-12-17 00:13:09
问题 x <- seq(0.1,10,0.1) y <- if (x < 5) 1 else 2 I would want the if to operate on every single case instead of operating on the whole vector. What do I have to change? 回答1: x <- seq(0.1,10,0.1) > x [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 [16] 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 [31] 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 [46] 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 [61] 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7

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

送分小仙女□ 提交于 2019-12-16 23:01:53
问题 In R, I have an element x and a vector v . I want to find the first index of an element in v that is equal to x . I know that one way to do this is: which(x == v)[[1]] , but that seems excessively inefficient. Is there a more direct way to do it? For bonus points, is there a function that works if x is a vector? That is, it should return a vector of indices indicating the position of each element of x in v . 回答1: The function match works on vectors : x <- sample(1:10) x # [1] 4 5 9 3 8 1 6 10

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

假装没事ソ 提交于 2019-12-16 23:01:50
问题 In R, I have an element x and a vector v . I want to find the first index of an element in v that is equal to x . I know that one way to do this is: which(x == v)[[1]] , but that seems excessively inefficient. Is there a more direct way to do it? For bonus points, is there a function that works if x is a vector? That is, it should return a vector of indices indicating the position of each element of x in v . 回答1: The function match works on vectors : x <- sample(1:10) x # [1] 4 5 9 3 8 1 6 10

How can I Vectorize this For Loop in MATLAB Code?

寵の児 提交于 2019-12-14 04:09:53
问题 I have the for loop (outlined below) in my code which takes a while to run. CALC is a function I have defined; Dis a matrix; Y is a matrix; k is a vector. Is there a way I can vectorize this code such that I do away with the for loop? Any contribution will be highly appreciated. for column = 1:n q(:,column) = CALC(D,Y(:,column), k(column)); end The CALC function is outlined below: function [x] = CALC(A, y, s) [m, n] = size(A); % y is an m x 1 vector % s is an integer r = y; index_cols = [];

Multiply matrices layer by layer

谁都会走 提交于 2019-12-14 03:26:57
问题 I want to do this without loops: % A ~ 4x2x3; B ~ 4x3x2; C ~ 4x2x2; for i=1:4 C(i,:,:) = squeeze(A(i,:,:))*squeeze(B(i,:,:)); end Thanks! 回答1: Haven't benchmarked this (so this is not guaranteed to be faster), but here goes: [L, ma, na] = size(A); [L, mb, nb] = size(B); AX = reshape(permute(A, [2 1 3]), [], na); BX = reshape(permute(B, [2 3 1]), mb, []); CX = reshape(permute(reshape(AX * BX, ma, L, nb, L), [1 3 2 4]), ma, nb, []); C = permute(CX(:, :, 1:L + 1:end), [3 1 2]); Note that you

Writing 'as.data.frame' method for custom S3 objects

最后都变了- 提交于 2019-12-14 03:26:17
问题 In many variants, similar questions have been asked many times ... but i do not find a clear advice about: "exporting S3 methods as functions" I wrote a custom S3 class with roxygen2 , call it 'my_item' . This is the constructor function: my_item <- function(n) structure(list(n=n),class='my_item') What I need is a way to define a "list of my_items => data.frame" cast function: #' @method as.data.frame my_item #' @export as.data.frame.my_item <- function(x) ... As soon as I call it with a my

Filling zeros in numpy array that are between non-zero elements with the same value

ぃ、小莉子 提交于 2019-12-14 02:20:07
问题 I have a 1D numpy numpy array with integers, where I want to replace zeros with the previous non-zero value if and only if the next non-zero value is the same. For example, an array of: in: x = np.array([1,0,1,1,0,0,2,0,3,0,0,0,3,1,0,1]) out: [1,0,1,1,0,0,2,0,3,0,0,0,3,1,0,1] should become out: [1,1,1,1,0,0,2,0,3,3,3,3,3,1,1,1] Is there a vectorized way to do this? I found some way to fill values of zeros here, but not how to do it with exceptions, i.e. to not fill the zeros that are within

Iterating without for loop in numpy array

人走茶凉 提交于 2019-12-14 01:42:32
问题 I need to do logical iteration over numpy array, which's values depend on elements of other array. I've written code below for clarifying my problem. Any suggestions to solve this problem without for loop? Code a = np.array(['a', 'b', 'a', 'a', 'b', 'a']) b = np.array([150, 154, 147, 126, 148, 125]) c = np.zeros_like(b) c[0] = 150 for i in range(1, c.size): if a[i] == "b": c[i] = c[i-1] else: c[i] = b[i] 回答1: Here's an approach using a combination of np.maximum.accumulate and np.where to