2x2 matrix multiplied by a 2x1 column vector gives erratic results

For example: A=[3,-2;2,-2] times v=[1;-1] works, but fails if A=[1,2;3,4]. The problem seems to be that in Matlab matrix multiplication the elements in row A are multiplied by the corresponding columns in B. Here B has only one column, and needs that the column elements in A be multiplied by the corresponding row elements in B. I have circumvented this problem by writing a function that does the latter, but as the need is for applying a vector to a transformation matrix, I am surprised to discover that the standard matrix multiplication algorithm cannot be relied upon. When it fails it takes A=[a1,b1;a2,b2] and computes v1a1+v2b1;v1a2+v2b2] instead of v1(a1+a2);v2(b1+b2). Is there away around this problem other than my function?

3 Commenti

What EXACTLY do you mean by "fails if A=[1,2;3,4]"? Give a complete self-contained example of what you try to do (something we can copy and paste into MATLAB and run), show what MATLAB computes, and explain exactly why you consider that to be an incorrect answer.
I think I figured out what he wants and commented on my answer. He wants the elements in the first column of A to each be multiplied by the first row in v (just one element) and be summed together. Then the elements in the second column of A to each be multiplied by the second row in v and be summed together. These two results then should be assembled in a column vector. I think when he says it "fails" he means it does not do what he wants (even though what he wants is not correct matrix multiplication and MATLAB is not failing).
"doing the correct calculation" is an interesting definition of "fail": MATLAB correctly returns the mathematically universal matrix product. How is this very basic mathematical operation either "erratic" or a "fail" ?

Accedi per commentare.

Risposte (1)

That is not how matrix multiplication works at all. For a matrix and a vector:
A= [1 2 v= [1
3 4] -1]
A*v= [1*1+2*(-1)
3*1+4*(-1)]
MATLAB does not fail. It does it correctly.
Please review matrix multiplication:

4 Commenti

It does it by multiplying each element in rows in A by the corresponding column elements in B. When there is only one column in B, it uses it for each row. Strictly, that is correct, as it follows what matlab says vector multiplication does. But in my case A is column vectors, not row vectors, and each element in a column needs to be multiplied by the corresponding row (one element) in B. This for finding a change in vector from a transformation matrix. I do not see a way of doing this in matlab, but my function for it works. That having been said, I find that A*v sometimes gives me the answer I want, and sometimes it gives the 'correct' answer, so I can't trust it.
ok, for what you want (which is not matrix multiplication) you could do the following:
A=[1,2;3,4];
v=[1;-1];
result=sum(A,1)'.*v
which gives:
result =
4
-6
I like your solution, as I learned a couple of things new. I have also resolved the "erratic" behavior I encountered. The matrix I was interested in -- [3,2;-2,-2] multiplied by [1;-1] just happens to give the same result as your solution to my problem as does the function I wrote. Too much sameness in the second column. Many thanks for your help!
@Timothy Goldsmith: please give us complete examples of when "I find that A*v sometimes gives me the answer I want, and sometimes it gives the 'correct' answer". We need to see complete working examples of this, i.e. the input and output matrices, and the actual and expected output matrices.

Accedi per commentare.

Commentato:

il 7 Giu 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by