vectorization

Applying operation to unevenly split portions of numpy array

会有一股神秘感。 提交于 2019-12-12 20:32:52
问题 I have three 1D numpy arrays: A list of times at which some measurements occurred ( t ). A list of measurements that occurred at each of the times in t ( y ). A (shorter) list of times for some some external changes that affected these measurements ( b ). Here is an example: t = np.array([0.33856697, 1.69615293 , 1.70257872 , 2.32510279, 2.37788203, 2.45102176, 2.87518307, 3.60941650, 3.78275907 , 4.37970516 , 4.56480259, 5.33306546, 6.00867792, 7.40217571, 7.46716989, 7.6791613 , 7.96938078,

z score with nan values in matlab (vectorized)

北城余情 提交于 2019-12-12 19:17:53
问题 I am trying to calculate the zscore for a vector of 5000 rows which has many nan values. I have to calculate this many times so I dont want to use a loop, I was hoping to find a vectorized solution. the loop solution: for i = 1:end vec(i,1) = (val(i,1) - nanmean(:,1))/nanstd(:,1) end a partial vectorized solution: zscore(vec(find(isnan(vec(1:end) == 0)))) but this returns a vector the length of the original vector minus the nan values. Thus it isn't the same as the original size. I want to

How to vectorize comparison in pandas dataframe?

对着背影说爱祢 提交于 2019-12-12 18:29:46
问题 I have a part of dataframe df like this: | nr | Time | Event | |----|------|-------| | 70 | 8 | | | 70 | 0 | | | 70 | 0 | | | 74 | 52 | | | 74 | 12 | | | 74 | 0 | | I want to assign events to the last column. The first entry is 1 by default. If Time[i] < 7 and nr[i] != nr[i-1], then Event[i]=Event[i-1]+1. If Time[i] < 7 and nr[i] = nr[i-1], then Event[i]=Event[i-1] If Time[i] > 7 then Event[i]=Event[i-1]+1. How do I effectively vectorize this? I want to avoid loops. 回答1: In your definition of

How to plot continuous colored vector field in Matlab?

最后都变了- 提交于 2019-12-12 17:34:19
问题 I have 2 matrices A and B , and by quiver(A,B) I can easily plot a vector field. However, does anyone know how to visual vector field in the following way in Matlab?(don't really know the name of this kind of plot) Thanks for helping me out! 回答1: If you are looking for a map of magnitude (velocities), then: v = sqrt( A.^2 + B.^2 ); figure; imagesc( v ); colormap jet;colorbar; axis image; Alternatively, if you want results that encode magnitude and direction in HSV color space like this

How to vectorize comparing each row of matrix with all other rows

血红的双手。 提交于 2019-12-12 16:35:53
问题 I am trying to compare each row with all other rows in a matrix to count the number of differences of each row with all other rows. The result is then stored in the bottom left triangle of a matrix. So for example when row m[1,] is compared with rows m[2,] and m[3,] the difference counts are stored at positions of mat[c(2:3), 1] in the result matrix. My problem is that my input matrix can have upto 1e+07 rows and the current implementation (speed and memory) will not scale due to n^2

How to perform operations along a certain dimension of an array?

假装没事ソ 提交于 2019-12-12 16:14:55
问题 I have a 3D array containing five 3-by-4 slices, defined as follows: rng(3372061); M = randi(100,3,4,5); I'd like to collect some statistics about the array: The maximum value in every column. The mean value in every row. The standard deviation within each slice. This is quite straightforward using loops, sz = size(M); colMax = zeros(1,4,5); rowMean = zeros(3,1,5); sliceSTD = zeros(1,1,5); for indS = 1:sz(3) sl = M(:,:,indS); sliceSTD(indS) = std(sl(1:sz(1)*sz(2))); for indC = 1:sz(1) rowMean

vectorize selection of ranges on a 1D vector in Matlab

冷暖自知 提交于 2019-12-12 16:11:18
问题 This is probably very simple, but I can't figure it out... I want to create a matrix of ranges and I can do this using the following loop: a=[0 10 22 35 42]; % sample initial ranges for i=1:length(a) b(i,:)= a(i):a(i)+5; end b = 0 1 2 3 4 5 10 11 12 13 14 15 22 23 24 25 26 27 35 36 37 38 39 40 42 43 44 45 46 47 How can it be vectorized? 回答1: a = 0:10:40; b = bsxfun(@plus,a', 0:5) b = 0 1 2 3 4 5 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 回答2: Both of the following

vectorization of “cumulative” regression

為{幸葍}努か 提交于 2019-12-12 15:34:54
问题 I have data dat <- data.frame(t=1:100,y=rnorm(100),x1=rnorm(100)),x2=rnorm(100)) where t gives points in time. I would like to regress y on x1 and x2 at each point in time based on the preceeding points in time. I could create a loop reg <- matrix(rep(NA,3*nrow(dat),ncol=3) for(i in 11:nrow(dat)){ reg[i,] <- coefficients(lm(y ~ x1 + x2, data=dat[1:i,])) } but I wonder whether anyone knows a way to vectorize this, perhaps using data.table . 回答1: We can use a non-equi-self-join to get the table

ARM Neon in C: How to combine different 128bit data types while using intrinsics?

久未见 提交于 2019-12-12 15:34:02
问题 TLTR For arm intrinsics, how do you feed a 128bit variable of type uint8x16_t into a function expecting uint16x8_t ? EXTENDED VERSION Context: I have a greyscale image, 1 byte per pixel. I want to downscale it by a factor 2x. For each 2x2 input box, I want to take the minimum pixel. In plain C, the code will look like this: for (int y = 0; y < rows; y += 2) { uint8_t* p_out = outBuffer + (y / 2) * outStride; uint8_t* p_in = inBuffer + y * inStride; for (int x = 0; x < cols; x += 2) { *p_out =

R data.table, accessing a matrix inside an assignment function

妖精的绣舞 提交于 2019-12-12 14:32:20
问题 I've the following data.table structure(list(xi = c(1, 1, 1, 2, 2, 2, 3, 3, 3), yi = c(1, 2, 3, 1, 2, 3, 1, 2, 3), flag = c(0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("xi", "yi", "flag"), row.names = c(NA, -9L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x11a1a78>) I also have a 3x3 matrix as below. structure(c(1, 1, 0.4, 1, 0, 0, 1, 0, 0.2), .Dim = c(3L, 3L)) I want to assign a third column to the data.table flag such that if the element in the matrix represented by