# MethodError: no method matching*Vector{Float64}

Hi everyone,
I know there are similar topics but I couldn’t fix my issue by reading them.
I get this error:
MethodError: no method matching *(::Vector{Float64}, ::Vector{Float64})

And here is my code:

``````function Qbandit(params, data)
Beta = params
LR = 0.5 + 0.5 * erf(params / sqrt(2))
W = params

r1::Array{Int64,1} = data.r1
r2::Array{Int64,1} = data.r2
C::Array{Int64,1} = data.Choice
TrialType::Array{Int64,1} = data.TrialType
Feedback::Array{Int64,1} = data.Feedback
Condition::Array{Int64,1} = data.Condition

# Initialize Q values

Q = zeros(typeof(Beta),4, 4)
conditions = ["condition1", "condition2", "condition3", "condition4"]
rownames = ["QA1", "QB1", "QA2", "QB2"]
Q = NamedArray(Q, (rownames, conditions), ("rows", "conditions"))

# Initialize lik
lik = 0

# loop over trials
for t in 1:length(C)
QC = Condition[t]

#increment likelihood according to log probability of making their actual choice
QATotal = (1-W)*(Q["QA1",QC]) - W*Q["QA2",QC]
QBTotal = (1-W)*(Q["QB1",QC]) - W*Q["QB2",QC]

if C[t] == 1
lik += lik + (Beta*QATotal - log(sum(exp.(Beta*c(QATotal, QBTotal)))))

elseif C[t] == 2
lik += lik + (Beta*QBTotal - log(sum(exp.(Beta*c(QATotal, QBTotal)))))
end

# Learning
if TrialType[t] == 1 && Feedback[t] == 1
if C[t] == 1
Q["QA1", QC] = (1 - LR) * Q["QA1", QC] + LR * r1[t]
elseif C[t] == 2
Q["QB1", QC] = (1 - LR) * Q["QB1", QC] + LR * r2[t]
end
elseif TrialType[t] == 1 && Feedback[t] == 0
if C[t] == 1
Q["QB1", QC] = (1 - LR) * Q["QB1", QC] + LR * r2[t]
elseif C[t] == 2
Q["QA1", QC] = (1 - LR) * Q["QA1", QC] + LR * r1[t]
end
elseif TrialType[t] == 0 && Feedback[t] == 1
if C[t] == 1
Q["QA2", QC] = (1 - LR) * Q["QA2", QC] + LR * r1[t]
elseif C[t] == 2
Q["QB2", QC] = (1 - LR) * Q["QB2", QC] + LR * r2[t]
end
elseif TrialType[t] == 0 && Feedback[t] == 0
if C[t] == 1
Q["QB2", QC] = (1 - LR) * Q["QB2", QC] + LR * r2[t]
elseif C[t] == 2
Q["QA2", QC] = (1 - LR) * Q["QA2", QC] + LR * r1[t]
end
end
end

return -lik
end

(betas,sigma,x,l,h) = em(data,subs,X,startbetas,startsigma,Qbandit; emtol=emtol, full=full);

``````

I’m not sure which line the error refers to, but I assumed this one:
lik += lik + (BetaQATotal - log(sum(exp.(Betac(QATotal, QBTotal)))))

I tried writing it in different ways but to no avail.

Hi!
It’s usually a good idea to format your code with three backticks at the beginning and three at the end, to make it easier to read. I haven’t read it but could it be that you’re trying to multiply two vectors elementwise? If so, you need to use the dot syntax for vectorization: `U .* V` instead of `U * V`

2 Likes

Thanks for the prompt response and the tip, it should be readable now.
I tried replacing U*V with U.*V everywhere in the code but it still gives that error.

Look at the stacktrace. You might be doing `Vector{Float64} * Vector{Float64}`, somewhere else. Maybe in function c ?

1 Like

Thanks, this was it. I’m using a package and traced it back to a function inside it.
Just one more thing, I’m still confused as to when I should use just * and when .* in julia.

Use `.` whenever you want the operation performed element-by-element. This is called broadcasting and Julia forces you to be explicit about when you want that behavior.

There is a macro available to make this a little more convenient in case you have many broadcasting operations in one line: `2 .* x .* y == @. 2 * x * y`.

See more detailed explanations here:

1 Like

Mathematically vector times vector is not defined. So julia doesn’t know what to do when you ask it do vector times vector. You can ask it to multiply each ith element of two vectors if that is your requirement. You can see all the methods of the `*` operator by running `methods(*)`.

1 Like

Thanks!