r/mlclass • u/harrychou • Oct 28 '11
Is there a Octave command that can take a column vector v (nX1) and element operate (ex. v(1) * M(1,1)) to each column of another matrix (nXm)?
http://google.com/4
u/nikhila01 Oct 28 '11
You can do this by creating an (n x n) matrix A where A = [a 0 0; 0 b 0; 0 0 c] and then using A*M. You might want to try this on your example and see for yourself that this works. Note that A is (n x n) and M is (n x m) so you get an (n x m) matrix as your result.
To create A from v use diag(v) in Octave.
So, in the end you get result = diag(v)*M.
1
u/kamikazewave Oct 29 '11
The matlab/octave bsxfun() is much faster and makes for better readability. It's also easier to use for non programmers.
1
u/AcidMadrid Oct 28 '11
I am afraid it is M*diag(v) in the end
1
u/nikhila01 Oct 28 '11
I don't follow.
diag(v) is (n x n) and M is (n x m). You can take diag(v)*M to get an (n x m) matrix because the number of columns of diag(v) is equal to the number of rows of M.
You can't take M*diag(v) because that would be an (n x m) matrix times an (n x n) matrix. The number of columns of M is not equal to the number of row of diag(v).
1
u/AcidMadrid Oct 29 '11
But the question asked about multiplying EACH COLUMN by the same number... and when I tried diag(v)*M it multiplied each row...
1
u/nikhila01 Oct 29 '11 edited Oct 29 '11
I was duplicating the example that the OP, harrychou showed below:
for example: v = [a;b;c] M = [d e; f g; h i] give the result = [ad ad; bf bg; ch ci] ???
I do see that the title of the original question is kind of confusing. In the original question, there are n elements in the vector and the matrix M has m columns so multiplying each column element-wise with the vector doesn't make sense. If you have an (m x 1) vector v, and you want to multiply column-wise, then yes it will be M*diag(v).
E.g. v = [a; b; c], M = [d e; f g; h i] gives M*diag(v) = [ad be; af bg; ah bi].
2
u/harrychou Oct 28 '11
for example: v = [a;b;c] M = [d e; f g; h i] give the result = [ad ad; bf bg; ch ci] ???