vectorization

How to convert matrix to a stack of diagonal matrices based on every row?

可紊 提交于 2020-01-11 10:17:09
问题 I have a matrix: A = [1 1 1 2 2 2 3 3 3] Is there a vectorized way of obtaining: B = [1 0 0 0 1 0 0 0 1 2 0 0 0 2 0 0 0 2 3 0 0 0 3 0 0 0 3] 回答1: Here's another way using sparse and repmat: A = [1 2 3; 4 5 6; 7 8 9]; A = A.'; B = full(sparse(1:numel(A), repmat(1:size(A,1),1,size(A,2)), A(:))); The original matrix is in A , and I transpose it so I can unroll the rows of each matrix properly for the next step. I use sparse to declare what is non-zero in a matrix. Specifically, we see that there

how to solve many overdetermined systems of linear equations using vectorized codes?

拥有回忆 提交于 2020-01-11 03:22:12
问题 I need to solve a system of linear equations Lx=b, where x is always a vector (3x1 array), L is an Nx3 array, and b is an Nx1 vector. N usually ranges from 4 to something like 10. I have no problems solving this using scipy.linalg.lstsq(L,b) However, I need to do this many times (something like 200x200=40000 times) as x is actually something associated with each pixel in an image. So x is actually stored in an PxQx3 array where P and Q is something like 200-300, and the last number '3' refers

Compute inverse of 2D arrays along the third axis in a 3D array without loops

末鹿安然 提交于 2020-01-11 02:11:43
问题 I have an array A whose shape is (N, N, K) and I would like to compute another array B with the same shape where B[:, :, i] = np.linalg.inv(A[:, :, i]) . As solutions, I see map and for loops but I am wondering if numpy provides a function to do this (I have tried np.apply_over_axes but it seems that it can only handle 1D array). with a for loop: B = np.zeros(shape=A.shape) for i in range(A.shape[2]): B[:, :, i] = np.linalg.inv(A[:, :, i]) with map : B = np.asarray(map(np.linalg.inv, np

Compute inverse of 2D arrays along the third axis in a 3D array without loops

こ雲淡風輕ζ 提交于 2020-01-11 02:10:54
问题 I have an array A whose shape is (N, N, K) and I would like to compute another array B with the same shape where B[:, :, i] = np.linalg.inv(A[:, :, i]) . As solutions, I see map and for loops but I am wondering if numpy provides a function to do this (I have tried np.apply_over_axes but it seems that it can only handle 1D array). with a for loop: B = np.zeros(shape=A.shape) for i in range(A.shape[2]): B[:, :, i] = np.linalg.inv(A[:, :, i]) with map : B = np.asarray(map(np.linalg.inv, np

Generate large number of random card decks - NumPy

与世无争的帅哥 提交于 2020-01-10 05:37:10
问题 I need to generate a large number of random poker card decks. Speed is important so everything has to be in numpy matrix form. I understand I can generate two cards from a deck as follows: np.random.choice(12*4,2, replace=False) How can I execute the same query so that a 2d array is created without a for loop? The difficulty is that each round will need to distribute from the original stack, so replace is only true for rows but False for columns. I've also tried it with originalDeck=np.arange

What's the fastest stride-3 gather instruction sequence?

。_饼干妹妹 提交于 2020-01-09 10:02:17
问题 The question: What is the most efficient sequence to generate a stride-3 gather of 32-bit elements from memory? If the memory is arranged as: MEM = R0 G0 B0 R1 G1 B1 R2 G2 B2 R3 G3 B3 ... We want to obtain three YMM registers where: YMM0 = R0 R1 R2 R3 R4 R5 R6 R7 YMM1 = G0 G1 G2 G3 G4 G5 G6 G7 YMM2 = B0 B1 B2 B3 B4 B5 B6 B7 Motivation and discussion The scalar C code is something like template <typename T> T Process(const T* Input) { T Result = 0; for (int i=0; i < 4096; ++i) { T R = Input[3

What's the fastest stride-3 gather instruction sequence?

左心房为你撑大大i 提交于 2020-01-09 10:02:14
问题 The question: What is the most efficient sequence to generate a stride-3 gather of 32-bit elements from memory? If the memory is arranged as: MEM = R0 G0 B0 R1 G1 B1 R2 G2 B2 R3 G3 B3 ... We want to obtain three YMM registers where: YMM0 = R0 R1 R2 R3 R4 R5 R6 R7 YMM1 = G0 G1 G2 G3 G4 G5 G6 G7 YMM2 = B0 B1 B2 B3 B4 B5 B6 B7 Motivation and discussion The scalar C code is something like template <typename T> T Process(const T* Input) { T Result = 0; for (int i=0; i < 4096; ++i) { T R = Input[3

What to replace loops and nested if sentences with in order to speed up Python code?

瘦欲@ 提交于 2020-01-07 03:16:05
问题 How can I avoid for loops and nested if sentences and be more Pythonic? At first glance this may seem like a "please do my all of my work for me" question. I can assure you that it is not. I'm trying to learn some real Python, and would like to discover ways of speeding up code based on a reproducible example and a pre-defined function. I'm calculating returns from following certain signals in financial markets using loads of for loops and nested if sentences. I have made several attempts,

What to replace loops and nested if sentences with in order to speed up Python code?

岁酱吖の 提交于 2020-01-07 03:15:09
问题 How can I avoid for loops and nested if sentences and be more Pythonic? At first glance this may seem like a "please do my all of my work for me" question. I can assure you that it is not. I'm trying to learn some real Python, and would like to discover ways of speeding up code based on a reproducible example and a pre-defined function. I'm calculating returns from following certain signals in financial markets using loads of for loops and nested if sentences. I have made several attempts,

Vectorizing 'for' loops

流过昼夜 提交于 2020-01-07 02:19:25
问题 This piece of code works as I want it to, but in the spirit of good MATLAB code, is there a way to vectorize this (previous is a k x 1 vector): start = zeros(k,1); for i = 2:length(previous) if (previous(i-1) == -1) start(previous(i))= start(previous(i))+1; end end What, in general, is the intuitive way to go about vectorizing code in MATLAB? 回答1: You can do this without find , for maximum performance: I = [false; previous(1:end-1) == -1]; idx = previous(I); start(idx) = start(idx) + 1; This