# Pattern matching in Matrix

**URL:** <https://discourse.julialang.org/t/pattern-matching-in-matrix/123831>\
**Category:** General Usage\
**Tags:** question, package, match, pattern-match\
**Created:** [December 14, 2024, 5:26am UTC](https://discourse.julialang.org/t/pattern-matching-in-matrix/123831 "2024-12-14T05:26:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 14, 2024, 5:26am UTC](https://discourse.julialang.org/t/pattern-matching-in-matrix/123831/1 "2024-12-14T05:26:02Z")

</div>

Find each instance of the smaller matrix in the larger and create a NamedTuple (;a,b)

```julia
[
    :X a _
    b _ :Y   
]

[
    1 2 3
    :X 1 2
    3 4 :Y
    :X 1 2
    4 5 :Y
    10 11 12
    :X 3 3
    7 4 :Y
    6 7 8
    8 7 6
]

#output 
[
(a=1,b=3)
(a=1,b=4)
(a=3,b=7)
]

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [December 14, 2024, 2:36pm UTC](https://discourse.julialang.org/t/pattern-matching-in-matrix/123831/2 "2024-12-14T14:36:17Z")

</div>

Guess, this needs a bit of help as you might want to match on overlapping/non-overlapping rows etc. Nevertheless, here is a way using `Match.jl`:

```julia-repl
julia> using IterTools, Match

julia> Z = [1 2 3; :X 1 2; 3 4 :Y; :X 1 2; 4 5 :Y; 10 11 12; :X 3 3; 7 4 :Y; 6 7 8; 8 7 6];

julia> foo(x) = @match x begin
           # Note: @match does not work on matrices directly 
           ([:X, a, _], [b, _, :Y]) => (; a, b)
           _ => missing
       end
foo (generic function with 1 method)

# Note: IterTools to get overlapping partitions of two rows
julia> skipmissing(foo.(IterTools.partition(eachrow(Z), 2, 1))) |> collect
3-element Vector{@NamedTuple{a::Int64, b::Int64}}:
 (a = 1, b = 3)
 (a = 1, b = 4)
 (a = 3, b = 7)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 15, 2024, 9:41pm UTC](https://discourse.julialang.org/t/pattern-matching-in-matrix/123831/3 "2024-12-15T21:41:32Z")

</div>

```julia
m=[
    :X :a 0
    :b 0 :Y   
]

M=[
    1 2 3
    :X 1 2
    3 4 :Y
    :X 1 2
    4 5 :Y
    10 11 12
    :X 3 3
    7 4 5
    :X 7 8
    8 7 :Y
]

#output 
[
(a=1,b=3)
(a=1,b=4)
(a=3,b=7)
]
rm,cm = size(m)
r,c=size(M)

AX=findall(e->e==:X ,M)
BY=AX.+CartesianIndex(rm-1,cm-1)
y=findall(e->M[e]==:Y,BY)

[(;a=M[x+CartesianIndex(0,1)],b=M[x+CartesianIndex(1,0)]) for x in AX[y]]

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 16, 2024, 8:34am UTC](https://discourse.julialang.org/t/pattern-matching-in-matrix/123831/4 "2024-12-16T08:34:11Z")

</div>

stretching a bit more in an attempt to be more general

```julia
[(;zip((m[1,2],m[2,1]),(M[x+CartesianIndex(0,1)],M[x+CartesianIndex(1,0)]))...) for x in AX[y]]

```
