Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I multiply the elements in each column, for every column in a matrix in MATLAB?

Tags:

matrix

matlab

For example, given the matrix

A = [ 1 2 3 ; 4 5 6; 7 8 9];

how do I multiply the column elements to get the result as result=[1*4*7 2*5*8 3*6*9]

like image 806
Learner Avatar asked May 05 '11 06:05

Learner


People also ask

How do you multiply corresponding elements in a matrix MATLAB?

C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.


2 Answers

Use the prod function with an optional argument indicating along which dimension the multiplication is to be carried out. For your case,

A=[ 1 2 3 ; 4 5 6; 7 8 9];
prod(A,1)

ans =

    28    80   162
like image 195
abcd Avatar answered Oct 13 '22 09:10

abcd


prod(A) gives you this result.

like image 25
ilalex Avatar answered Oct 13 '22 09:10

ilalex