# Is there a way to "choose the indices" for broadcasting?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-choose-the-indices-for-broadcasting/1317>\
**Category:** General Usage\
**Tags:** question, broadcast\
**Created:** [January 6, 2017, 1:56am UTC](https://discourse.julialang.org/t/is-there-a-way-to-choose-the-indices-for-broadcasting/1317 "2017-01-06T01:56:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 6, 2017, 1:56am UTC](https://discourse.julialang.org/t/is-there-a-way-to-choose-the-indices-for-broadcasting/1317/1 "2017-01-06T01:56:03Z")

</div>

As always, examples are best. With a loop I can do:

```julia
for i in 1:3:12
  tmp[i] = a[i]*b[i]
end

```

Is there a way to make

```julia
tmp .= a.*b

```

only update specific chosen indices? And as a slight extension, what about having `tmp` be dense while the others aren’t?

```julia
idxs = 1:3:12
for i in idxs
  tmp[i] = a[idxs[i]]*b[idxs[i]]
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 6, 2017, 2:02am UTC](https://discourse.julialang.org/t/is-there-a-way-to-choose-the-indices-for-broadcasting/1317/2 "2017-01-06T02:02:37Z")

</div>

You could use views, e.g. `view(tmp, 1:3:12) .= view(a, 1:3:12) .* view(b, 1:3:12)`.

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [January 6, 2017, 5:54am UTC](https://discourse.julialang.org/t/is-there-a-way-to-choose-the-indices-for-broadcasting/1317/3 "2017-01-06T05:54:42Z")

</div>

You could also do

```julia
I = 1:3:12
tmp[I] .= view(a, I) .* view(b, I)

```

This gets lowered to `broadcast!(*, dotview(tmp, I), view(a, I), view(b, I))`, where `dotview` is a `view` for `AbstractArrays` or `getindex` otherwise. I wonder whether it is worth to do the same for the rest of the arguments, in which case you could simply write `tmp[I] .= a[I] .* b[I]`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 6, 2017, 6:00am UTC](https://discourse.julialang.org/t/is-there-a-way-to-choose-the-indices-for-broadcasting/1317/4 "2017-01-06T06:00:30Z")

</div>

> [@PabloZubieta](#):
>
> in which case you could simply write tmp[I] .= a[I] .\* b[I].

That last syntax is beautiful. What about a macro for that?

```julia
@broadcast_view tmp[I] .= a[I] .* b[I]

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 20, 2017, 10:06pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-choose-the-indices-for-broadcasting/1317/5 "2017-01-20T22:06:14Z")

</div>

I’ve been thinking about this as well, and I’ve submitted a PR so that you can just put `@views` in front of an assignment, a whole block of code, or even a whole function, and every slice is converted to a view: [https://github.com/JuliaLang/julia/pull/20164](https://github.com/JuliaLang/julia/pull/20164)
