vectorization

How to Add a row vector to a column vector like matrix multiplication

故事扮演 提交于 2019-12-31 05:22:56
问题 I have a nx1 vector and a 1xn vector. I want to add them in a special manner like matrix multiplication in an efficient manner (vectorized): Example: A=[1 2 3]' B=[4 5 6] A \odd_add B = [1+4 1+5 1+6 2+4 2+5 2+6 3+4 3+5 3+6 ] Regards 回答1: You can use bsxfun: A=[1 2 3]' B=[4 5 6] bsxfun(@plus, A, B) The result is ans = 5 6 7 6 7 8 7 8 9 回答2: You can use the repmat function (replicate matrices): repmat(A,1,3)+repmat(B,3,1) 来源: https://stackoverflow.com/questions/11690743/how-to-add-a-row-vector

Unaligned access on aligned pointer?

末鹿安然 提交于 2019-12-31 05:11:43
问题 I have this function code: bool interpolate(const Mat &im, float ofsx, float ofsy, float a11, float a12, float a21, float a22, Mat &res) { bool ret = false; // input size (-1 for the safe bilinear interpolation) const int width = im.cols-1; const int height = im.rows-1; // output size const int halfWidth = res.cols >> 1; const int halfHeight = res.rows >> 1; float *out = res.ptr<float>(0); const float *imptr = im.ptr<float>(0); for (int j=-halfHeight; j<=halfHeight; ++j) { const float rx =

Unaligned access on aligned pointer?

馋奶兔 提交于 2019-12-31 05:11:14
问题 I have this function code: bool interpolate(const Mat &im, float ofsx, float ofsy, float a11, float a12, float a21, float a22, Mat &res) { bool ret = false; // input size (-1 for the safe bilinear interpolation) const int width = im.cols-1; const int height = im.rows-1; // output size const int halfWidth = res.cols >> 1; const int halfHeight = res.rows >> 1; float *out = res.ptr<float>(0); const float *imptr = im.ptr<float>(0); for (int j=-halfHeight; j<=halfHeight; ++j) { const float rx =

Speed up random number generation in MATLAB

﹥>﹥吖頭↗ 提交于 2019-12-31 04:56:24
问题 Is there any way to generate pseudo-random numbers to less precision and thus speed the process up? Another thing is that I know it saves time if random numbers are generated all at once (e.g. rand(100,1000) ), instead of one by one. Could someone explain why this is true? 回答1: MATLAB actually implements more than one random number generator. They differ significantly in terms of execution time and in terms of "randomness" (I think, but I didn't verify). However, I understand from your

Speed up random number generation in MATLAB

徘徊边缘 提交于 2019-12-31 04:56:05
问题 Is there any way to generate pseudo-random numbers to less precision and thus speed the process up? Another thing is that I know it saves time if random numbers are generated all at once (e.g. rand(100,1000) ), instead of one by one. Could someone explain why this is true? 回答1: MATLAB actually implements more than one random number generator. They differ significantly in terms of execution time and in terms of "randomness" (I think, but I didn't verify). However, I understand from your

Convert a for loop into a vector (vectorization)

做~自己de王妃 提交于 2019-12-31 03:43:09
问题 For those super experts out there, I was wondering if you see a quick way to convert the following "for" loop into a one-line vector calculation that is more efficient. %Define: %A size (n,1) %B size (n,m) %C size (n,1) B = [2 200; 3 300; 4 400]; C = [1;2;1]; for j=1:n A(j) = B( j, C(j) ); end So to be clear, is there any alternative way to express A, as a function of B and C, without having to write a loop? 回答1: Yes, there is: A = B(sub2ind([n,m], (1:n).', C)); 回答2: It depends on functions A

How to vectorize row-wise diagonalization of a matrix

谁说我不能喝 提交于 2019-12-31 02:55:28
问题 I have an n-by-m matrix that I want to convert to a mn-by-m matrix, with each m-by-m block of the result containing the diagonal of each row. For example, if the input is: [1 2; 3 4; 5 6] the output should be: [1 0; 0 2; 3 0; 0 4; 5 0; 0 6] Of course, I don't want to assemble the matrix step by step myself with a for loop. Is there a vectorized and simple way to achieve this? 回答1: For a vectorized way to do this, create the linear indices of the diagonal elements into the resulting matrix,

Matlab — random walk with boundaries, vectorized

别等时光非礼了梦想. 提交于 2019-12-31 02:14:11
问题 Suppose I have a vector J of jump sizes and an initial starting point X_0. Also I have boundaries 0, B (assume 0 < X_0 < B). I want to do a random walk where X_i = [min(X_{i-1} + J_i,B)]^+. (positive part). Basically if it goes over a boundary, it is made equal to the boundary. Anyone know a vectorized way to do this? The current way I am doing it consists of doing cumsums and then finding places where it violates a condition, and then starting from there and repeating the cumsum calculation,

Speed of vectorized operation dependent of number of columns of data.frame

狂风中的少年 提交于 2019-12-31 00:44:26
问题 Why does it take longer to operate a comparison on a data.frame with the same number of elements, but arranged in more columns on vectorized operations? Take this simple example, where we subtract 0.5 from each element and then compare it to see if it is < 0 ( related to this question ): f.df <- function( df , x = 0.5 ){ df <- df - x df[ df < 0 ] <- 0 return( df ) } df1 <- data.frame( matrix( runif(1e5) , nrow = 1e2 ) ) df2 <- data.frame( matrix( runif(1e5) , nrow = 1e3 ) ) df3 <- data.frame(

Speed of vectorized operation dependent of number of columns of data.frame

流过昼夜 提交于 2019-12-31 00:44:02
问题 Why does it take longer to operate a comparison on a data.frame with the same number of elements, but arranged in more columns on vectorized operations? Take this simple example, where we subtract 0.5 from each element and then compare it to see if it is < 0 ( related to this question ): f.df <- function( df , x = 0.5 ){ df <- df - x df[ df < 0 ] <- 0 return( df ) } df1 <- data.frame( matrix( runif(1e5) , nrow = 1e2 ) ) df2 <- data.frame( matrix( runif(1e5) , nrow = 1e3 ) ) df3 <- data.frame(