vectorization

How to avoid nested for loops in matlab?

别说谁变了你拦得住时间么 提交于 2019-12-24 16:52:36
问题 I am constructing an adjacency list based on intensity difference of the pixels in an image. The code snippet in Matlab is as follows: m=1; len = size(cur_label, 1); for j=1:len for k=1:len if(k~=j) % avoiding diagonal elements intensity_diff = abs(indx_intensity(j)-indx_intensity(k)); %intensity defference of two pixels. if intensity_diff<=10 % difference thresholded by 10 adj_list(m, 1) = j; % storing the vertices of the edge adj_list(m, 2) = k; m = m+1; end end end end y = sparse(adj_list(

How to avoid nested for loops in matlab?

£可爱£侵袭症+ 提交于 2019-12-24 16:51:18
问题 I am constructing an adjacency list based on intensity difference of the pixels in an image. The code snippet in Matlab is as follows: m=1; len = size(cur_label, 1); for j=1:len for k=1:len if(k~=j) % avoiding diagonal elements intensity_diff = abs(indx_intensity(j)-indx_intensity(k)); %intensity defference of two pixels. if intensity_diff<=10 % difference thresholded by 10 adj_list(m, 1) = j; % storing the vertices of the edge adj_list(m, 2) = k; m = m+1; end end end end y = sparse(adj_list(

How to effectively apply bitwise operation to (large) packed bit vectors?

冷暖自知 提交于 2019-12-24 15:21:48
问题 I want to implement void bitwise_and( char* __restrict__ result, const char* __restrict__ lhs, const char* __restrict__ rhs, size_t length); or maybe a bitwise_or() , bitwise_xor() or any other bitwise operation. Obviously it's not about the algorithm, just the implementation details - alignment, loading the largest possible element from memory, cache-awareness, using SIMD instructions etc. I'm sure this has (more than one) fast existing implementations, but I would guess most library

How to vectorize searching function in Matlab?

不羁的心 提交于 2019-12-24 14:08:04
问题 Here is a Matlab coding problem (A little different version with intersect not setdiff here: a rating matrix A with 3 cols, the 1st col is user'ID which maybe duplicated, 2nd col is the item'ID which maybe duplicated, 3rd col is rating from user to item, ranging from 1 to 5. Now, I have a subset of user IDs smallUserIDList and a subset of item IDs smallItemIDList , then I want to find the rows in A that rated by users in smallUserIDList , and collect the items that user rated, and do some

Vectorized solution to conditional dataframe selection

拟墨画扇 提交于 2019-12-24 14:02:39
问题 I recently asked a question which was answered - How do I add conditionally to a selection of cells in a pandas dataframe column when the the column is a series of lists?, but I believe have a new problem which I had not previously considered. In the following dataframe I need two conditions to result in a change to column d . Each value in column d is a list . Where a == b , the final integer in d is incremented by one. Where a != b , the list of integers is extended and the value 1 is

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 13:49:53
问题 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 #include<iostream> #include <Eigen/Core> // import most common Eigen types using namespace Eigen; int main(int, char *[]) { Matrix4f m3; m3 << 1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9,

Vectorized way of accessing row specific elements in a numpy array

為{幸葍}努か 提交于 2019-12-24 13:05:46
问题 I have a 2-D NumPy array and a set of indices the size of which is the first dimension of the NumPy array. X = np.random.rand(5, 3) a = np.random.randint(0, 3, 5) I need to do something like for i, ind in enumerate(a): print X[i][ind] Is there a vectorized way of doing this? 回答1: Here you go: X = np.random.rand(5, 3) a = np.random.randint(0, 3, 5) In [12]: X[np.arange(a.size), a] Out[12]: array([ 0.99653335, 0.30275346, 0.92844957, 0.54728781, 0.43535668]) In [13]: for i, ind in enumerate(a):

Vectorizing the Sum of a Triple For Loop

北战南征 提交于 2019-12-24 12:14:21
问题 Currently, I am trying to build the matrix Alpha through this triple-loop: Alpha = zeros(a_length, b_length); for a = 1:a_length for b = 1:b_length for c = 1:c_length Alpha(a,b) = Alpha(a,b) + Beta(c,a) * Gamma(b,c); end end end Is there a way to pass in two vectors to the Beta and Gamma Matrices, such that I can construct the entirety of Alpha in a single, vectorized line of elegant code? 回答1: You could use simple matrix multiplication to your rescue - Alpha = (Gamma*Beta).' Or this way -

How to simplify a leading-NA count function, and generalize it to work on matrix, dataframe

空扰寡人 提交于 2019-12-24 11:28:39
问题 I wrote a leading-NA count function, it works on vectors. However: a) Can you simplify my version? b) Can you also generalize it to work directly on matrix, dataframe (must still work on individual vector), so I don't need apply() ? Try to avoid all *apply functions, fully vectorize, it must still work on a vector, and no special-casing if at all possible. leading_NA_count <- function(x) { max(cumsum((1:length(x)) == cumsum(is.na(x)))) } # v0.1: works but seems clunky, slow and unlikely to be

How to vectorize code with nested if and loops in Python?

空扰寡人 提交于 2019-12-24 11:20:02
问题 I have a dataframe like given below df = pd.DataFrame({ 'subject_id' :[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2], 'day':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], 'PEEP' :[7,5,10,10,11,11,14,14,17,17,21,21,23,23,25,25,22,20,26,26,5,7,8,8,9,9,13,13,15,15,12,12,15,15,19,19,19,22,22,15] }) df['fake_flag'] = '' In this operation, I am performing an operation as shown below in code. This code works