Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row product of matrix and column sum of matrix

Tags:

r

matrix

I have a matrix of size TxR and I am looking for a command to do the product of the rows (returning an 1 x R vector of the products). After that I want to sum over the columns, i.e. sum the R terms.

In Matlab this would be done something like this sum(prod(A,1),2), but I don't know the code for this in R.

I hope it make sense.

Thanks

like image 234
Ku-trala Avatar asked Nov 25 '13 16:11

Ku-trala


People also ask

How can you get row total and column total of the matrix by matrix multiplication provide the codes?

To find the sum of row, columns, and total in a matrix can be simply done by using the functions rowSums, colSums, and sum respectively. The row sums, column sums, and total are mostly used comparative analysis tools such as analysis of variance, chi−square testing etc.

What is row sum of a matrix?

To calculate the sum of elements in each row: Two loops will be used to traverse the array where the outer loop selects a row, and the inner loop represents the columns present in the matrix a. Calculate the sum by adding elements present in a row. Display sumRow. Repeat this for each row.

How do you find the sum of a row in a matrix?

S = sum( A , dim ) returns the sum along dimension dim . For example, if A is a matrix, then sum(A,2) is a column vector containing the sum of each row.

Can a row matrix be multiplied by a column matrix?

A matrix having equal number of rows and columns is called a square matrix. So when a row matrix is multiplied by a column matrix both having the same number of elements, the resulting matrix formed is a square matrix.


1 Answers

sum(apply(A, 1, prod))

apply the prod function across the rows (the 1 is the margin), sum the result.

like image 115
jimmyb Avatar answered Oct 19 '22 02:10

jimmyb