matrix

Weighted rowSums of a matrix

房东的猫 提交于 2020-01-11 11:09:25
问题 I have a matrix like this: I would like to sum every value of a single row but weighted. Example: Given a specific row, the sum would be: S = x1 * loan + x2 * mortdue + x3 * value + ... x1, x2, x3, ... are predefined values. I tried rowSums() and things like that but I have not been able to figure out how to do it properly. 回答1: You are looking for a matrix-vector multiplication. For example, if you have a matrix: set.seed(0) A <- matrix(round(rnorm(9), 1), 3) # [,1] [,2] [,3] #[1,] 1.3 1.3

Multiple single channel matrix converted to single multi channel matrix

↘锁芯ラ 提交于 2020-01-11 10:55:15
问题 i am working in opencv c++ api with matrices I have 4 single channel Mat that i will like to merge into one 4 channel matrix. It is basically the rgba channels i have in 4 matrices and want to combine into one rgba image/matrix. Anyone who knows how to do that? 回答1: You can use cv::merge to do what you want. One possible usage: cv::Mat r,g,b,a; //Fill r,g,b,a with data cv::Mat result; std::vector<cv::Mat> channels; channels.push_back(r); channels.push_back(g); channels.push_back(b); channels

How do I implement a circular buffer in Python?

痞子三分冷 提交于 2020-01-11 10:48:12
问题 I have a matrix for instance a=[12,2,4,67,8,9,23] and I would like a code that appends a value say 45 to it and removes the first value '12' so in essence I want to make a = [2,4,67,8,9,23,45] I want to work with regular matrices not numpy matrices so I can't use hstack or vstack How do I do this in python? Any help would be appreciated, thanks 回答1: The simplest way: a = a[1:] + [45] 回答2: Use a deque. http://docs.python.org/2/library/collections.html#collections.deque >>> import collections >

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

Get Pure View Matrix in Vuforia

久未见 提交于 2020-01-11 10:13:29
问题 I am using Vuforia SDK to build my AR app. By using trackableResult->getPose(), I can get a model view matrix of the target frame marker. But I also need pure view matrix to do some calculations. Is there any way to get it? 回答1: To follow on from peedee's comment above, here is a picture I found extremely helpful, linked to from this web page. Sorry I couldn't leave this as a comment, but they don't allow pictures in comments. Note that different texts may use slightly different names for

Sparse Multi-Dimensional Data Representation

天涯浪子 提交于 2020-01-11 10:13:26
问题 I'm working on a cardiac simulation tool that uses 4-dimensional data, i.e. several (3-30) variables at locations in 3D space. I'm now adding some tissue geometry which will leave over 2/3 of the points in the containing 3D box outside of the tissue to be simulated, so I need a way to efficiently store the active points and not the others. Crucially, I need to be able to: Iterate over all of the active points within a constrained 3D box (iterator, perhaps?) Having accessed one point, find its

Matlab Convert Vector to Binary Matrix [duplicate]

让人想犯罪 __ 提交于 2020-01-11 09:12:11
问题 This question already has answers here : Create a zero-filled 2D array with ones at positions indexed by a vector (4 answers) Closed 3 years ago . I have a vector v of size (m,1) whose elements are integers picked from 1:n. I want to create a matrix M of size (m,n) whose elements M(i,j) are 1 if v(i) = j, and are 0 otherwise. I do not want to use loops, and would like to implement this as a simple vector-matrix manipulation only. So I thought first, to create a matrix with repeated elements M

Find the most repeated row in a matrix

我的梦境 提交于 2020-01-11 09:11:31
问题 I have about 10000 replicates of a sample in a matrix. My matrix has 1000 rows and 6 columns. Numbers in the columns range from 0:58 depending on the sample. actual.prob <- c(.14, .14, .16, .13, .19, .24) million.rep <- replicate(10000, sample(1:6, 58, replace= T, actual.prob)) new.matrix <- matrix(nrow= 10000, ncol=6) for(i in 1:10000){ new.matrix[i,] <- as.vector(table(factor(million.rep[,i], levels=1:6))) } new.matrix[1:10,] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 3 7 11 11 11 15 [2,] 7 6 12 5

Find the most repeated row in a matrix

爷,独闯天下 提交于 2020-01-11 09:11:28
问题 I have about 10000 replicates of a sample in a matrix. My matrix has 1000 rows and 6 columns. Numbers in the columns range from 0:58 depending on the sample. actual.prob <- c(.14, .14, .16, .13, .19, .24) million.rep <- replicate(10000, sample(1:6, 58, replace= T, actual.prob)) new.matrix <- matrix(nrow= 10000, ncol=6) for(i in 1:10000){ new.matrix[i,] <- as.vector(table(factor(million.rep[,i], levels=1:6))) } new.matrix[1:10,] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 3 7 11 11 11 15 [2,] 7 6 12 5

Find the most repeated row in a matrix

自作多情 提交于 2020-01-11 09:11:26
问题 I have about 10000 replicates of a sample in a matrix. My matrix has 1000 rows and 6 columns. Numbers in the columns range from 0:58 depending on the sample. actual.prob <- c(.14, .14, .16, .13, .19, .24) million.rep <- replicate(10000, sample(1:6, 58, replace= T, actual.prob)) new.matrix <- matrix(nrow= 10000, ncol=6) for(i in 1:10000){ new.matrix[i,] <- as.vector(table(factor(million.rep[,i], levels=1:6))) } new.matrix[1:10,] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 3 7 11 11 11 15 [2,] 7 6 12 5