# Is there a shorter way to write \`for m₁ in axes(M, 1), m₂ in axes(M, 2)\` in the co

**URL:** <https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543>\
**Category:** General Usage\
**Created:** [March 5, 2021, 11:03am UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543 "2021-03-05T11:03:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 5, 2021, 11:03am UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543/1 "2021-03-05T11:03:19Z")

</div>

Is there a shorter way to write `for m₁ in axes(M, 1), m₂ in axes(M, 2)` in the code below? m₁ and m₂ are required.

```julia
let M = [1 2 3; 4 5 6]
	res = Any[]
	for m₁ in axes(M, 1), m₂ in axes(M, 2)
		push!(res, (M[m₁, m₂]))
	end
	res
end

```

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1614942176400300?thread_ts=1614942176.400300&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 5, 2021, 6:09pm UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543/2 "2021-03-05T18:09:19Z")

</div>

`for m in CartesianIndices(M)` and then use `m[1]` and `m[2]` in your code.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 5, 2021, 6:15pm UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543/3 "2021-03-05T18:15:59Z")

</div>

> [@BridgeBot](#):
>
> `res = Any[]`

Consider carefully whether you should use `Any` here, instead of some concrete type. It is likely to hurt performance significantly.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 5, 2021, 7:11pm UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543/4 "2021-03-05T19:11:44Z")

</div>

Just to be sure, the `CartesianIndices` provide in this case a shorter syntax but they seem to be 2x less performant in terms of allocations and speed compared to OP’s proposal? Tested it for: `M = randn(1_000,1_000)`

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 5, 2021, 7:27pm UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543/5 "2021-03-05T19:27:56Z")

</div>

They should be essentially the same on Julia 1.6 or higher. Which version are you testing?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 5, 2021, 7:28pm UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543/6 "2021-03-05T19:28:44Z")

</div>

Julia Version 1.6.0-beta1.0 on Win10. Copying test here in case I might be doing something wrong:

```julia
M = randn(1_000,1_000)

@btime let # 74 ms (2979998 allocations: 70 MiB)
	res = Any[]
	for m₂ in axes(M, 2), m₁ in axes(M, 1)
		push!(res, (M[m₁, m₂]))
	end
end

@btime let # 146 ms (4978020 allocations: 146 MiB)
    res = Any[]
    for m in CartesianIndices(M)
        m₁, m₂ = m[1], m[2]
        push!(res, M[m])
    end
end

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 5, 2021, 7:35pm UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543/7 "2021-03-05T19:35:02Z")

</div>

It’s typically easier to just benchmark functions rather than `let`s… but the key is that you need to flag `M` as an _argument_ to the benchmark kernel with a `$` either way. Otherwise, it’s treated as a non-constant global reference, which can have strange performance effects depending upon how inlining happens.

```nohighlight
julia> @btime let # 74 ms (2979998 allocations: 70 MiB)
               res = Any[]
               for m₂ in axes($M, 2), m₁ in axes($M, 1)
                       push!(res, ($M[m₁, m₂]))
               end
       end
  11.836 ms (1000020 allocations: 24.26 MiB)

julia> @btime let # 146 ms (4978020 allocations: 146 MiB)
           res = Any[]
           for m in CartesianIndices($M)
               m₁, m₂ = m[1], m[2]
               push!(res, $M[m])
           end
       end
  11.574 ms (1000020 allocations: 24.26 MiB)

```

Of course, now we still have the pessimized storage for the `Any[]` array, but that just adds ~1ms to each.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 5, 2021, 9:10pm UTC](https://discourse.julialang.org/t/is-there-a-shorter-way-to-write-for-m-in-axes-m-1-m-in-axes-m-2-in-the-co/56543/8 "2021-03-05T21:10:33Z")

</div>

@mbauman, thank you for the explanation.  
`pairs()` seems to have similar performance in this case: `for (m, Mij) in pairs(M)` and then use `m[1]`, `m[2]` and `Mij`
