0

We've got a columnvector m x 1 and a matrix m x n. For the value in row i in the columnvector we want to multiply this value with each value in the same row i of the matrix, and then sum all of these up. This is to be repeated for every row i in the vector so that we end up with a columnvector.

Want to do this with a for-loop, have this so far (where M is the matrix and v is the initial columnvector we start out with) which returns an error that says "Subscripted assignment dimension mismatch.", so I guess I messed up with my indices somehow:

for i = 1:nv
    for k = 1:mM
        columnvectorendresult(i,) = columnvectorendresult(i,) + v(i,:)*M(i,:); 
    end
end

Don't know if I'm close with what I have so far, but not fully into this just yet. Any suggestions?

5
  • Hm. This must be one of those: v.'*M, M.'*v, my brain is not on 100% today. Commented Mar 14, 2015 at 14:21
  • You are indexing a vector with two indices olumnvectorendresult(i,k), that can't be correct. Please provide a minimal example, from your description it's not clear if you expect a mx1 or 1xn output. Commented Mar 14, 2015 at 14:28
  • The expected output is a columnvector with 1 column and m rows, so mx1. Yeah removed k in columnvectorendresult(i,k) now, that makes sense, it should not have more than 1 column so it can't equal k which is set to mM. Commented Mar 14, 2015 at 14:30
  • 2
    Ok. It's not the a matrix product then. just v.*sum(M,2) Commented Mar 14, 2015 at 14:32
  • @knedlsepp Post that as an answer! Commented Mar 14, 2015 at 16:03

2 Answers 2

1

In case you want to sum after multiplication, the answer of knedlsepp using the distributive property of multiplication is the logical choice. If you want to use other operations than sums or differences, than the following answer can be applied more generically

Here we go:

%// columnvector m x 1
a = randi(5,3,1)
%// matrix m x n
B = randi(5,3,2)

%// multiplication
Ba = bsxfun(@times,B,a(:))

%// sum
BaSum = sum(Ba,2)

Example:

a =
     3
     4
     4


B =
     2     5
     3     1
     1     1


Ba =
     6    15
    12     4
     4     4


BaSum =
    21
    16
     8
Sign up to request clarification or add additional context in comments.

3 Comments

Distributive property should reduce this to a.*sum(B,2).
This worked out really good, thanks a loads! a.*sum(B,2) is really nice in its simplicity: multiply all elements of a elementwise, and sum up across dimension 2, which are the columns Saw around that bsxfun is popular, guess its the most time/resource efficient way?
@Mollart bsxfun is one of the most efficient functions implemented. For everything which can't be solved with basic operations it is always a very good option.
1

Instead of multiplying each entry with the same factor and then doing the summation, you should sum the rows of the matrix first and then do the multiplication. ("Use the distributive property of multiplication.")

This is how you do this in MATLAB:

columnvectorendresult = v.*sum(M,2);

1 Comment

One should know that after 6 years of studying engineering :p shame on me and +1 for you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.