# Issue with comparing two vectors with different sizes

**URL:** https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177
**Category:** General Usage
**Created:** [August 23, 2022, 8:24am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177 "2022-08-23T08:24:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [August 23, 2022, 8:24am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177/1 "2022-08-23T08:24:48Z")

</div>

I am wondering how I can get results for

```julia
R = [2, 2]
D = [1, 3]
J = [1, 1]
P[2] = [1 3; 2 3; 6 3; 1 4; 2 4; 6 4; 1 5; 2 5; 6 5; 1 6; 2 6]
P[2][:,2] .== collect(R[2]+1:R[2]+D[2])

```

similar to

```julia
R = [2, 2]
D = [1, 3]
J = [1, 1]
P[1] = [1 3; 2 3; 4 3; 1 4; 2 4]
P[1][:,2] .== collect(R[1]+1:R[1]+D[1])

```

that I got

```julia
5-element BitVector:
 1
 1
 1
 0
 0

```

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [August 23, 2022, 8:37am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177/2 "2022-08-23T08:37:09Z")

</div>

What result do you expect for the first piece of code with `P[2]`?

You can for eg. do:

```julia
julia> P[2][:,2] .== permutedims(R[2]+1:R[2]+D[2])
11×3 BitMatrix:
 1 0 0
 1 0 0
 1 0 0
 0 1 0
 0 1 0
 0 1 0
 0 0 1
 0 0 1
 0 0 1
 0 0 0
 0 0 0

julia> sum(P[2][:,2] .== permutedims(R[2]+1:R[2]+D[2]), dims = 2)
11×1 Matrix{Int64}:
 1
 1
 1
 1
 1
 1
 1
 1
 1
 0
 0

```

---

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [August 23, 2022, 9:16am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177/3 "2022-08-23T09:16:59Z")

</div>

> [@digital\_carver](#):
>
> `sum(P[2][:,2] .== permutedims(R[2]+1:R[2]+D[2]), dims = 2)`

thanks for your response, it solved this issue but since I then want to use

```julia
findall(sum(P[1][:,2] .== permutedims(R[1]+1:R[1]+D[1]), dims = 2)
                .& (P[1][:,1] .== collect(R1]+D[1]+1:R[1]+D[1]+J[1])))

```

that produce

```julia
TypeError: non-boolean (Int64) used in boolean context

```

while the previous version

```julia
findall((P[1][:,2] .== collect(R[1]+1:R[1]+D[1]))
                .& (P[i][:,1] .== collect(R[1]+D[1]+1:R[1]+D[1]+J[1])))

```

was OK.

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [August 23, 2022, 9:26am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177/4 "2022-08-23T09:26:47Z")

</div>

Use `vec(any(...))` instead of the `sum`:

```julia
julia> vec(any(P[2][:,2] .== permutedims(R[2]+1:R[2]+D[2]), dims = 2))
11-element BitVector:
 1
 1
 1
 1
 1
 1
 1
 1
 1
 0
 0

julia> findall(vec(any(P[2][:,2] .== permutedims(R[2]+1:R[2]+D[2]), dims = 2)))
9-element Vector{Int64}:
 1
 2
 3
 4
 5
 6
 7
 8
 9

```

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [August 23, 2022, 9:30am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177/5 "2022-08-23T09:30:27Z")

</div>

All of that allocates a lot though, unnecessarily. Now that I understand what you’re looking for, here’s a simpler and faster way to do this:

```julia
julia> P2[:, 2] .∈ Ref(R[2]+1:R[2]+D[2])
11-element BitVector:
 1
 1
 1
 1
 1
 1
 1
 1
 1
 0
 0

```

`findall` should work with this too.

---

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [August 23, 2022, 9:47am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177/6 "2022-08-23T09:47:21Z")

</div>

> [@digital\_carver](#):
>
> `vec(any`

Thanks a lot. I’m trying to produce following matrices

```julia
for i in 1:2
    [1:size(P[i],1) P[i][[
            findall(P[i][:,2] .== collect(R[i]+D[i]+1:R[i]+D[i]+J[i])); 
            findall((P[i][:,2] .== permutedims(R[i]+1:R[i]+D[i]))
                .& (P[i][:,1] .== collect(R[i]+D[i]+1:R][i]+D[i]+J[i]))); 
            findall((P[i][:,2] .== collect(R[i]+1:R[i]+D[i]))
                .!= (P[i][:,1] .== collect(R[i]+D[i]+1:R[i]+D[i]+J[i])))], :]]
end

```

but, I can get response for `P[1]`, but got the mentioned error for `P[2]`!

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [August 23, 2022, 9:53am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177/7 "2022-08-23T09:53:38Z")

</div>

Can you explain what you’re trying to do? There’s probably a simpler and more readable and maintainable way to write this.

---

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [August 23, 2022, 10:01am UTC](https://discourse.julialang.org/t/issue-with-comparing-two-vectors-with-different-sizes/86177/8 "2022-08-23T10:01:23Z")

</div>

> [@digital\_carver](#):
>
> `vec(any(P[2][:,2] .== permutedims(R[2]+1:R[2]+D[2]), dims = 2))`

I’m trying to model a water network for max flow problem using a type of adjacent matrices. first column become number of pipes and second and third columns become “from” and “to” nodes. What makes the problem problematic is three type of nodes that must be distinguished and considered during the formulation and also coding.

I got this for first network

```julia
5×3 Matrix{Int64}:
 1 1 4
 2 2 4
 3 4 3
 4 1 3
 5 2 3

```

and trying to get similar for the second one as

```julia
11×3 Matrix{Int64}:
  1 1 6
  2 2 6
  3 1 3
  4 2 3
  5 6 3
  6 1 4
  7 2 4
  8 6 4
  9 1 5
 10 2 5
 11 6 5

```

What is important is the order of number of nodes in the second and third columns
