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
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.
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.
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.
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.
sum(apply(A, 1, prod))
apply the prod function across the rows (the 1 is the margin), sum the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With