Reputation: 77096
I was quite puzzled by the following,
sqrt(1:3) * [1 2 3]
# 3x3 Matrix, as expected
sqrt(1:3) * 1:3
# error `colon` has no method matching...
until I realised that 1:3 must be a different kind of beast, i.e not just a vector as I expected from Matlab. My current workaround is to use hcat
to convert it to a vector, sqrt(1:3) * hcat(1:3...)
, is there a better approach?
Upvotes: 2
Views: 849
Reputation: 11644
The main problem with the second version
sqrt(1:3) * 1:3
is actually operator precedence. The colon operator is very low precedence, so this translates to
(sqrt(1:3) * 1):3
which is nonsensical, hence the error
ERROR: `colon` has no method matching colon(::Array{Float64,1}, ::Int64)`
Having said that, if you "fix it" with parentheses it doesn't work because the operator isn't defined. Hence you probably want sqrt(1:3) * [1:3]'
.
Upvotes: 5
Reputation: 304
typeof(1:3)
gives UnitRange{Int64} (constructor with 1 method)
, whereas typeof([1:3])
gives: Array{Int64,1}
. Note that [1:3]
is by default a column vector, so you need to transpose it: sqrt(1:3) * [1:3].'
Upvotes: 2