Why does .* create a matrix when multiplying a row vector by a column vector
Show older comments
I am a longtime matlab user and somehow only now running into this issue that seems like strange behavior to me. When multiplying one dimensional vectors element wise, I don't expect there to be a difference between column and row vectors since they are both 1-D. Yet, when you multiply a row vector using .* by a column vector you do not get a 1D vector you get a matrix.
a=1:3;
b=[2
2
2];
c=a.*b
c =
2 4 6
2 4 6
2 4 6
Why does this happen? After some reading I learned matlab has 'implicit expansion' which is what I assume is happening. But I guess my deeper question is why is this a feature? If this is the result I wanted, I would do actual vector multiplication, b*a which gives the same result.
I am currently making a function that accepts vector inputs, and now I have to add some lines to check to make sure that all inputs are the correct orientation. To be specific, I am trying to calculate [1 2 3] .* [2 2 2]=[2 4 6] and have my answer be a vector. A matrix result would be meaningless in my case. I see the otions as either manually varify that the vectors passed to the function are same orientation, or have code that checks the orientation of each line (using size() for example) and transpose vectors as necessary. I am frustrated that these are the only options and wondering if there is a quicker solution.
Accepted Answer
More Answers (1)
Why does this happen? After some reading I learned matlab has 'implicit expansion' which is what I assume is happening. But I guess my deeper question is why is this a feature? If this is the result I wanted, I would do actual vector multiplication, b*a which gives the same result.
There have been long discussions in the forum whether this feature of implicit expansion is useful or not and whether it may hide errors in programming. The majority of people voted that the advantages overweigh the disadvantages. I think we shouldn't open a new discussion about the usefulness of this feature.
I am currently making a function that accepts vector inputs, and now I have to add some lines to check to make sure that all inputs are the correct orientation.
So you don't trust in your programming or the inputs provided by anonymous users of your function ?
3 Comments
Evan
on 6 Dec 2022
I would like my function to be agnostic to the inputs being column/row to make it easier for me to use later.
So independent of the orientation of a and b, you want to get a.*b as a and b being multiplied elementwise ? But here is where the problem begins: should the resulting a.*b be a row or a column vector ? If you only use the result in your own code, it might not be important for later use. But if you use it in a MATLAB solver, e.g., it will almost always matter - even if the implicit expansion feature had not been introduced.
Evan
on 6 Dec 2022
Categories
Find more on Linear Algebra in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!