# DimensionMismatch("arrays could not be broadcast to a common size")

i have this function for computing a probability

``````function probability(eta,pdc,x::Array,y::Array)
P=[]
for k in 1:length(y)
if y[k]==0
push!(P,(1-pdc)*(1-eta*(1-pdc))^x[k])
elseif y[k]==1
push!(P,1-(1-pdc)*(1-eta*(1-pdc))^x[k])
end
P_iDC=prod.(P[2:end-2])
P_DC=sum.(P_iDC)
P_iECDC=prod.(P)
P_ECDC=sum.(P_iECDC)
print(P_ECDC./P_DC)
end
end
probability(0.05,1e-5,[1,0,1,1,0,1,0,1],[1,1,0,0,1,0,0,0])
``````

when the function is called it shows me this error that i don’t know really how to fix it:

``````Any[]

DimensionMismatch("arrays could not be broadcast to a common size")

Stacktrace:
 probability(::Float64, ::Float64, ::Array{Int64,1}, ::Array{Int64,1}) at ./In:15
 top-level scope at In:18

``````

from the error message the error is in this line
`print(P_ECDC./P_DC)`
but i don’t know the way to fix it

I haven’t figured out what this function is supposed to do, so I’m going to try to annotate this.

``````function probability(eta,pdc,x::Array,y::Array)
P=[]
for k in 1:length(y)
# You are pushing scalars into the array P
if y[k]==0
push!(P,(1-pdc)*(1-eta*(1-pdc))^x[k])
elseif y[k]==1
push!(P,1-(1-pdc)*(1-eta*(1-pdc))^x[k])
end
# You are broadcasting prod over an array of scalars
# Prod of a scalar is the same scalar
# The below is equivalent to P_iDC = P[2:end-2]
# At k = 2, P[2:end-2] === P[2:1] === []
# P_iDC has length of 0
# Maybe you mean prod(P[2:end-2])
P_iDC=prod.(P[2:end-2])
# P_DC is also []
P_DC=sum.(P_iDC)
# P_iECDC also is just P, maybe you meant prod(P)
P_iECDC=prod.(P)
# I think you mean sum(P_iECDC)
P_ECDC=sum.(P_iECDC)
# At k =2, you are trying to divide an array of length 2 by an array of length 0, error!
print(P_ECDC./P_DC)
end
end
probability(0.05,1e-5,[1,0,1,1,0,1,0,1],[1,1,0,0,1,0,0,0]) # Consider using Bool[1,0,1,1,0,1,0,1]
``````

OK, let’s back up here and go over what the broadcasting operator `.` does.

``````julia> A = [1,2,3,4]
4-element Array{Int64,1}:
1
2
3
4

julia> prod.(A)
4-element Array{Int64,1}:
1
2
3
4

julia> [ prod(A), prod(A), prod(A), prod(A) ] # broadcasting prod over A
4-element Array{Int64,1}:
1
2
3
4

julia> prod(A)
24

julia> sum(A)
10

julia> sum.(A)
4-element Array{Int64,1}:
1
2
3
4

julia> [ sum(A), sum(A), sum(A), sum(A) ] # sum.(A) is equivalent to this statement
4-element Array{Int64,1}:
1
2
3
4
``````
1 Like

This is my best guess of what you are trying to do:

``````julia> function probability(eta,pdc,x::Array,y::Array)
P=Float64[]
for k in 1:length(y)
if y[k]==0
push!(P,(1-pdc)*(1-eta*(1-pdc))^x[k])
elseif y[k]==1
push!(P,1-(1-pdc)*(1-eta*(1-pdc))^x[k])
end
P_iDC=prod(P[2:end-2])
P_DC=sum(P_iDC)
P_iECDC=prod(P)
P_ECDC=sum(P_iECDC)
println(P_ECDC/P_DC)
end
end
probability (generic function with 1 method)

julia> probability(0.05,1e-5,[1,0,1,1,0,1,0,1],[1,1,0,0,1,0,0,0]) # Consider using Bool[1,0,1,1,0,1,0,1]
0.05000900000499997
5.000900000477238e-7
4.750809992328367e-7
0.04513226735418804
4.7508099923283667e-7
4.750809992328366e-7
0.047507624842500644
0.04750762484250064
``````
1 Like

i’m expecting only one probability as an outcome from this function since `prod` and `sum` of an array will give us a scalar then you take the outcome of `P_ECDC` and `P_DC`and you divide them on each other

for example if `x`is a multiple array like this: x=[[2, 1, 0, 0, 0, 0, 1, 0], [1, 1, 1, 0, 0, 0, 1, 0]], [[0, 1, 1, 0, 0, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1, 0], [0, 1, 0, 0, 0, 1, 1, 1], [1, 1, 0, 0, 0, 1, 1, 0]] in this case i’m expecting an array with same length as `x` which is in this case `2`

``````julia> x=[[2, 1, 0, 0, 0, 0, 1, 0], [1, 1, 1, 0, 0, 0, 1, 0]], [[0, 1, 1, 0, 0, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1, 0], [0, 1, 0, 0, 0, 1, 1, 1], [1, 1, 0, 0, 0, 1, 1, 0]]
([[2, 1, 0, 0, 0, 0, 1, 0], [1, 1, 1, 0, 0, 0, 1, 0]], [[0, 1, 1, 0, 0, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1, 0], [0, 1, 0, 0, 0, 1, 1, 1], [1, 1, 0, 0, 0, 1, 1, 0]])

julia> typeof(x)
Tuple{Array{Array{Int64,1},1},Array{Array{Int64,1},1}}

julia> length(x)
2

julia> x
2-element Array{Array{Int64,1},1}:
[2, 1, 0, 0, 0, 0, 1, 0]
[1, 1, 1, 0, 0, 0, 1, 0]

julia> x
4-element Array{Array{Int64,1},1}:
[0, 1, 1, 0, 0, 1, 1, 0]
[1, 1, 0, 0, 0, 1, 1, 0]
[0, 1, 0, 0, 0, 1, 1, 1]
[1, 1, 0, 0, 0, 1, 1, 0]
``````

OK, so `x` is now a tuple of arrays of arrays? How about `y`?

1 Like

Let’s just walk through this line by line:

``````julia> eta = 0.05
0.05

julia> pdc = 1e-5
1.0e-5

julia> x = [1,0,1,1,0,1,0,1]
8-element Array{Int64,1}:
1
0
1
1
0
1
0
1

julia> y = [1,1,0,0,1,0,0,0]
8-element Array{Int64,1}:
1
1
0
0
1
0
0
0

julia> P = Float64[]
Float64[]

julia> k = 1
1

julia> y[k]
1

julia>                    push!(P,1-(1-pdc)*(1-eta*(1-pdc))^x[k])
1-element Array{Float64,1}:
0.05000900000499997

julia> P[2:end-2]   # P[2:-1] is empty!!!
Float64[]

julia>                P_iDC=prod(P[2:end-2])
1.0

julia>                P_DC=sum(P_iDC)
1.0

julia>                P_iECDC=prod(P)
0.05000900000499997

julia>                P_ECDC=sum(P_iECDC)
0.05000900000499997

julia>                println(P_ECDC/P_DC)
0.05000900000499997

julia> k = 2
2

julia> y[k]
1

julia>                    push!(P,1-(1-pdc)*(1-eta*(1-pdc))^x[k])
2-element Array{Float64,1}:
0.05000900000499997
9.99999999995449e-6

julia> P[2:end-2]
Float64[]

julia>                P_iDC=prod(P[2:end-2])
1.0

julia>                P_DC=sum(P_iDC)
1.0

julia>                P_iECDC=prod(P)
5.000900000477238e-7

julia>                P_ECDC=sum(P_iECDC)
5.000900000477238e-7

julia>                println(P_ECDC/P_DC)
5.000900000477238e-7

julia> k = 3
3

julia> y[k]
0

julia>                    push!(P,(1-pdc)*(1-eta*(1-pdc))^x[k])
3-element Array{Float64,1}:
0.05000900000499997
9.99999999995449e-6
0.949990999995

julia> P[2:end-2]
Float64[]

julia>                P_iDC=prod(P[2:end-2])
1.0

julia>                P_DC=sum(P_iDC)
1.0

julia>                P_iECDC=prod(P)
4.750809992328367e-7

julia>                P_ECDC=sum(P_iECDC)
4.750809992328367e-7

julia>                println(P_ECDC/P_DC)
4.750809992328367e-7

julia> k = 4
4

julia> y[k]
0

julia>                    push!(P,(1-pdc)*(1-eta*(1-pdc))^x[k])
4-element Array{Float64,1}:
0.05000900000499997
9.99999999995449e-6
0.949990999995
0.949990999995

julia> P[2:end-2]
1-element Array{Float64,1}:
9.99999999995449e-6

julia>                P_iDC=prod(P[2:end-2])
9.99999999995449e-6

julia>                P_DC=sum(P_iDC)
9.99999999995449e-6

julia>                P_iECDC=prod(P)
4.513226735398264e-7

julia>                P_ECDC=sum(P_iECDC)
4.513226735398264e-7

julia>                println(P_ECDC/P_DC)
0.04513226735418804

julia> # a few more cycles

julia> nothing # Your function currently returns nothing

``````

My current best guess:

``````julia> function probability(eta,pdc,x::Array,y::Array)
P=Float64[]
for k in 1:length(y)
if y[k]==0
push!(P,(1-pdc)*(1-eta*(1-pdc))^x[k])
elseif y[k]==1
push!(P,1-(1-pdc)*(1-eta*(1-pdc))^x[k])
end
end
P_iDC=prod(P[2:end-2])
P_DC=sum(P_iDC)
P_iECDC=prod(P)
P_ECDC=sum(P_iECDC)
P_ECDC/P_DC
end
probability (generic function with 1 method)

julia> probability(0.05,1e-5,[1,0,1,1,0,1,0,1],[1,1,0,0,1,0,0,0])
0.04750762484250064
``````

yes, `y`is a simple array