7

Multiply each row of a matrix with its transpose

 2 years ago
source link: https://www.codesd.com/item/multiply-each-row-of-a-matrix-with-its-transpose.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Multiply each row of a matrix with its transpose

advertisements

The formula I have to translate to Octave/Matlab goes something like this:

\sum (v_i - m) (v_i - m)^T

I have a matrix, and I need to take each row, subtract m from it and then multiply it with its own transpose. I wrote the inner part as a function:

function w = str(v, m)
    y = v - m
    w = y * transpose(y)
end

My matrix is like this

xx = [1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5]

Now I have no idea how to apply this function to each row in a matrix and then sum them up to a new matrix. Maybe someone can help me here.

EDIT: The result is not the dot product. I'm looking for v * v^T, which has a matrix as result!


Probably you need this

X = bsxfun( @minus, A, m );
Y = X'* X;

Related Articles

How to multiply each row in a matrix by each column of the second matrix of Python?

I know the steps to multiply two matrices are as follow Step 1: Make sure that the the number of columns in the 1st one equals the number of rows in the 2nd one. Step 2: Multiply the elements of each row of the first matrix by the elements of each co

Multiply the values ​​of each row of the matrix with columnvector and sum rows

We've got a columnvector m x 1 and a matrix m x n. For the value in row i in the columnvector we want to multiply this value with each value in the same row i of the matrix, and then sum all of these up. This is to be repeated for every row i in the

armadillo c ++: efficient and concise way to multiply each row of a matrix by a vector?

I am wondering if there is an efficient and concise way to do an element-wise multiplication of every row (or column) of an Armadillo C++ matrix by a vector. The row (or column) and vector are the same size. For example, IF fmat::each_row() (and/or e

How to multiply each row by each row of another matlab element matrix?

I have such a matrix m1 = [ 1 2 3; 4 5 6; 1 2 3] m2 = [ 2 2 2]; and I want to multiply each row of m1 with m2 elementwise . So result is result = [2 4 6; 8 10 12; 2 4 6] How would I do it? bsxfun(@times,m1,m2) ans = 2 4 6 8 10 12 2 4 6

Find the column index for the 1 in each row of a matrix

I have the following matrix in Matlab: M = [0 0 1 1 0 0 0 1 0 1 0 0 0 0 1]; Each row has exactly one 1. How can I (without looping) determine a column vector so that the first element is a 2 if there is a 1 in the second column, the second element is

How to divide each row of a matrix by elements of a vector in R

I would like to divide each row of a matrix by a fixed vector. For example mat<-matrix(1,ncol=2,nrow=2,TRUE) dev<-c(5,10) Giving mat/dev divides each column by dev. [,1] [,2] [1,] 0.2 0.2 [2,] 0.1 0.1 However, I would like to have this as a result,

How to apply a function to each row of a matrix (or a data frame) in R

Suppose I have a n by 2 matrix and a function that takes a 2-vector as one of its arguments. I would like to apply the function to each row of the matrix and get a n-vector. How to do this in R? For example, I would like to compute the density of a 2

How to repeat a calculation for each row of a matrix in R?

I am very very new to programming and R. I have tried to find an answer to my question, but part of the problem is I don't know exactly what to search. I am trying to repeat a calculation (statistical distance) for each row of a matrix. Here is what

the fastest method to keep the first 2 values ​​of each row in a matrix (or data.frame) in R

Assume I have the matrix M and I just want to keep the 2 highest values of each row in these matrix and others will be set to zero. M <- rbind(c(0.1, 0.6, 0.2, 0.3, 0.7), c(0.8, 0.1, 0.7, 0.2, 0.4)) > M [,1] [,2] [,3] [,4] [,5] [1,] 0.1 0.6 0.2 0.3

subtracting a constant vector from each row of a matrix in r

This question already has an answer here: How to divide each row of a matrix by elements of a vector in R 2 answers I have a matrix with 5 columns and 4 rows. I also have a vector with 3 columns. I want to subtract the values in the vector from colum

Count the sub-lines in each row of a matrix in Matlab?

I need an algorithm in Matlab which counts how many adjacent and non-overlapping (1,1) I have in each row of a matrix A mx(n*2) without using loops. E.g. A=[1 1 1 0 1 1 0 0 0 1; 1 0 1 1 1 1 0 0 1 1] %m=2, n=5 Then I want B=[2;3] %mx1 Specific case As

Access each row of a matrix and apply to the function

I have this matrix in r which has two columns, shape and scale. Now i have 10000 rows, what i need is to apply this code below: rw <- rweibull(10, shape=, scale=) I need to loop through each row of the matrix in order to calculate the rw. Any help wo

A: How do I add a column with a randomly chosen value for each row in a matrix?

I'll preface this by saying I'm an R noob and that I think this may have an easy solution, but I'm struggling to find it. I've got a matrix with 2 columns and 1,000 rows. Keeping the rows fixed, I'd like to create a new variable that randomly chooses

Multiply each column into a matrix per column in the vector

sampleInput = 2.1053 -4.8512 4.6223 0.9665 1.0000 hiddenWeights = -0.6342 -0.2089 0.4533 -0.6182 -0.3663 -0.9465 -1.0770 -0.2668 0.7077 -1.1656 0.0936 -0.2853 -0.1408 0.6193 -0.5481 1.4253 0.3770 -0.6710 0.1069 0.0310 I want the result to be hiddenWe

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK