Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab multiply each row in matrix by different number

Say that I have a matrix:

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

Is there a way to multiply :
row 1 by 1
row 2 by 2
row 3 by 3
and so on?

I am able to do this with for loops, however it if for an assignment where they want us to use matrices. In the actual assignment A is filled with random number but each row which by multiplied consecutively.

Thanks, any help is much appreciated

like image 383
Jonski Goldstein Avatar asked Oct 19 '16 01:10

Jonski Goldstein


People also ask

Can you multiply matrices with different number of rows?

You can only multiply two matrices if their dimensions are compatible , which means the number of columns in the first matrix is the same as the number of rows in the second matrix.

How do I multiply certain rows in MATLAB?

Description. 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.

How do you multiply each number in an array in MATLAB?

B = prod( A ) returns the product of the array elements of A . If A is a vector, then prod(A) returns the product of the elements. If A is a nonempty matrix, then prod(A) treats the columns of A as vectors and returns a row vector of the products of each column. If A is an empty 0-by-0 matrix, prod(A) returns 1 .


1 Answers

You just need to multiply a diagonal matrix by A like so.

A = [ 1 2 3 ; 4 5 6 ; 7 8 9 ; 10 11 12];
disp(diag([1 2 3 4]) * A);

 1     2     3
 8    10    12
21    24    27
40    44    48
like image 146
pscuderi Avatar answered Nov 15 '22 06:11

pscuderi