Matrix dimension not agree in matrix vector operation

I'm trying to perform matrix vector element wise operation for example
rand(3) - [1,2,3]
I get error of Matrix dimensions must agree. However, this would work on my friend's machine. Similarly
rand(3) .* [1,2,3]
would not work on my machine but on hers. I'm expecting same behavior as bsxfun(@times, rand(3), [1,2,3]) My Matlab version is 2015b and hers' is 2016b. I thought this was supported long time ago?

Answers (2)

Hello Christine, One of the quirkier aspects of Matlab is that rand(n) and some similar functions give you a square matrix, 3x3 in this case, so neither of the commands you show will to work. You can't subtract a row vector from a matrix, at least not with that syntax. It is not clear what is happening with your friend's setup but those commands will not work in Matlab 2016b either.
The feature where some operators (like the minus operator - ) automatically perform implicit expansion was introduced in release R2016b. You can read more about it on Loren's blog. For earlier releases you will need to use bsxfun.

This question is closed.

Products

Asked:

on 30 Oct 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!