vectorization

Direction of two points

蹲街弑〆低调 提交于 2019-12-20 16:23:53
问题 Some high school math concept has been forgotten, so I ask here. If I have two points p1(x1,y1) , p2(x2,y2) , the direction is P1-->p2 , that's p1 points to p2 . To represent this direction by vector, is it Vector(x2-x1,y2-y1) or Vector(x1-x2, y1-y2) ? By the way, what is the purpose to normalize a vector? 回答1: Answer 1: it is Vector(x2-x1,y2-y1) Answer 2: Normalizing means to scale the vector so that its length is 1. It is a useful operation in many computations, for example, normal vectors

Direction of two points

主宰稳场 提交于 2019-12-20 16:23:05
问题 Some high school math concept has been forgotten, so I ask here. If I have two points p1(x1,y1) , p2(x2,y2) , the direction is P1-->p2 , that's p1 points to p2 . To represent this direction by vector, is it Vector(x2-x1,y2-y1) or Vector(x1-x2, y1-y2) ? By the way, what is the purpose to normalize a vector? 回答1: Answer 1: it is Vector(x2-x1,y2-y1) Answer 2: Normalizing means to scale the vector so that its length is 1. It is a useful operation in many computations, for example, normal vectors

How to vectorize R strsplit?

本秂侑毒 提交于 2019-12-20 09:39:19
问题 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 lengths of words in a character vector: words <- c("a","quick","brown","fox") > length(strsplit(words,"")) [1] 4 # The number of words (length of the list) >

Intel SSE and AVX Examples and Tutorials [closed]

不想你离开。 提交于 2019-12-20 08:03:03
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Is there any good C/C++ tutorials or examples for learning Intel SSE and AVX instructions? I found few on Microsoft MSDN and Intel sites, but it would be great to understand it from the basics.. 回答1: For the visually inclined SIMD programmer, Stefano Tommesani's site is the best introduction to x86 SIMD

Structure initialization with repmat

放肆的年华 提交于 2019-12-20 07:51:37
问题 I want to initalize structures and it seems to be too slow. How do I do it with repmat , which is supposed to be a much faster solution in Matlab? Originally: for i=1:30 myloc.one.matrixBig(i,1).matrixBig= zeros(6,6); for j=1:5 myloc.one.id(i,j) = 0; for k=1:10 myloc.one.final(j,k).final(i,1) = 0; end end end EDIT: for j=1:30 for i=1:10 myObject{i,j}.s = zeros(6,1); myObject{i,j}.f = zeros(6,1); end end Also, am I able to make it faster by adding some [] initialization even before, or is that

Ineffective remainder loop in my code

人盡茶涼 提交于 2019-12-20 07:29:00
问题 I have this function: 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 = ofsx +

matlab: filling matrix diagonalwise [duplicate]

夙愿已清 提交于 2019-12-20 06:21:14
问题 This question already has answers here : adding values to diagonals of matrix using element-wise addition in matlab (3 answers) Closed 4 years ago . I have an (2n-1)-by-1 vector with certain values and I want to obtain an n-n matrix with the diagonals filled using the same value. Eg. if I have a = [1; 2; 3; 4; 5]; I want to obtain A = [[3 4 5];[2 3 4];[1 2 3]] = 3 4 5 2 3 4 1 2 3 My matrix dimensions are a lot bigger so I'd want this as efficient as possible. I already found following

Insert new values into an array

Deadly 提交于 2019-12-20 05:35:12
问题 I currently have a column vectors of different lengths and I want to insert another column vector at various points of the original array. i.e. I want to add my new array to the start of the old array skip 10 places add my new array again, skip another 10 spaces and add my new array again and so on till the end of the array. I can do this by using: OffsetSign = [1:30]'; Extra = [0;0;0;0;0]; OffsetSign =[Extra;OffsetSign(1:10);Extra;OffsetSign(11:20);Extra;OffsetSign(21:30)]; However this is

3d Matrix to 2d Matrix matlab

╄→尐↘猪︶ㄣ 提交于 2019-12-20 04:50:46
问题 I am using Matlab R2014a. I have a 3-dimensional M x N x M matrix A. I would like a vectorized way to extract a 2 dimensional matrix B from it, such that for each i,j I have B(i,j)=A(i,j,g(i,j)) where g is a 2-dimensional index matrix of size M x N, i.e. with integral values in {1,2,...,M}. The context is that I am representing a function A(k,z,k') as a 3-dimensional matrix, the function g(k,z) as a 2-dimensional matrix, and I would like to compute the function h(k,z)=f(k,z,g(k,z)) This seems

NumPy: Dot product for many small matrices at once

青春壹個敷衍的年華 提交于 2019-12-20 04:28:12
问题 I have a long array of 3-by-3 matrices, e.g., import numpy as np A = np.random.rand(25, 3, 3) and for each of the small matrices, I would like to perform an outer product dot(a, a.T) . The list comprehension import numpy as np B = np.array([ np.dot(a, a.T) for a in A ]) works, but doesn't perform well. A possible improvement could be to do just one big dot product, but I'm having troubles here setting up A correctly for it. Any hints? 回答1: You can obtain the list of transposed matrices as A