Trying to collect indices with multiple conditions

Hello all,

I am trying to find all the indicies where certain criteria is met, here is my code

ind1 = Vector{Float64}(undef, 4)

for i in 1:4
ind1[i] = intersect(findall(x->x==l, z), findall(sum(M[i,j]*z[j] for j in items)+sum(N[i,j]*w[j] for j in items)+q[i])>0)
end

It is giving me error

TypeError: non-boolean (Float64) used in boolean context

Any help or guidance will be appreciated :slight_smile:

Thank you

You can use 3 backticks ``` to display formatted code. After a bit of formatting, the second condition looks like you misplaced a parenthesis. You wrote of findall( formula ) > 0 for the second condition, I think you meant findall(formula > 0) as below:

ind1 = Vector{Float64}(undef, 4)

for i in 1:4
    ind1[i] = intersect(
        findall(x->x==l, z),
        findall(sum(M[i,j]*z[j] for j in items)+sum(N[i,j]*w[j] for j in items)+q[i]>0),
    )
end
1 Like

Oh ok, thanks alot, but now it is giving me this error :slight_smile: .

MethodError: Cannot convert an object of type Vector{Int64} to an object of type Int64

P.S I have converted ind1 = Vector{Int64}(undef, 4)

Right, intersect is returning a vector of indices and you are attempting to assign it to a single Int64 in your ind1 vector. Try

ind1 = map(1:4) do i
    intersect(
        findall(x->x==l, z),
        findall(sum(M[i,j]*z[j] for j in items)+sum(N[i,j]*w[j] for j in items)+q[i]>0),
    )
end

That should result in a Vector{Vector{Int64}}.

1 Like

Nope this still doesnt work, See the follwoing code:

l = [0, 0, 0, 0]
z = [0,1,1,1]
ind1 = map(1:4) do i
    findall(x->x==l, z)
end

The answer should be: ind1 = [1] as at only index one we have z=l. but it isnt, it gives me [0,0,0,0] :frowning: