# Why doesn't this view assignment work?

**URL:** <https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030>\
**Category:** New to Julia\
**Tags:** images\
**Created:** [June 25, 2020, 9:15am UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030 "2020-06-25T09:15:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiggunjer](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jiggunjer](https://discourse.julialang.org/u/jiggunjer)\
**Post date:** [June 25, 2020, 9:15am UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/1 "2020-06-25T09:15:18Z")

</div>

Hello. I am using the Images package to do some processing. I have an RGB image `img`, and I’d like to create a view such that I can index channels by name. My idea was to derive view slices from the channelView:

```julia
cv = channelview(img)
myview = Dict("red" => (@view cv[1, :, :]), "green" => (@view cv[2, :, :]), "blue" => (@view cv[3, :, :]))
#Rotate the red channel 180 degrees:
myview["red"] = imrotate(myview["red"], pi, axes(img), 0) #converting error 

```

Can someone explain why this doesn’t work, yet the `cv[1,:,:]` equivalent does?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [June 25, 2020, 9:57am UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/2 "2020-06-25T09:57:37Z")

</div>

The `Dict` value type is inferred as array view. So, you cannot assign arrays into it, only 2D views of 3D arrays. You can modify the elements in-place, however, if the dimensions won’t change:

```julia
myview["red"] .= imrotate(myview["red"], pi, axes(img), 0)

```

---

<div class="post-metadata">

**Author:** ![jiggunjer](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jiggunjer](https://discourse.julialang.org/u/jiggunjer)\
**Post date:** [June 25, 2020, 10:00am UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/3 "2020-06-25T10:00:56Z")

</div>

And the type of `cv[1,:,:]` is not inferred as array view? How would I change the `Dict` value type so that the assignment in my example works (assuming dimensions don’t change)?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [June 25, 2020, 10:58am UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/4 "2020-06-25T10:58:23Z")

</div>

Slices like `cv[1,:,:]` make copies that are independent from the parent array. Views produce objects that are indexed just like arrays, but share data with the parent array.

See this manual section:  
[https://docs.julialang.org/en/v1/manual/performance-tips/#Consider-using-views-for-slices-1](https://docs.julialang.org/en/v1/manual/performance-tips/#Consider-using-views-for-slices-1)

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [June 25, 2020, 1:34pm UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/5 "2020-06-25T13:34:13Z")

</div>

And regarding the question of how to change the `Dict` type, that depends.  
If you want the changes to the dict values to reflect on the original `cv`, you should have the dict as it is and modify its elements in-place. If you just want to split the image into three channels, then use `cv[i,:,:]` - as you have noticed, it will work, but you may need additional work to join the channels back into one image.

---

<div class="post-metadata">

**Author:** ![jiggunjer](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jiggunjer](https://discourse.julialang.org/u/jiggunjer)\
**Post date:** [June 25, 2020, 4:03pm UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/6 "2020-06-25T16:03:10Z")

</div>

I read that part of the documentation. But in my case I’m talking about the lefthand side slice, i.e. non-copy assignment. The purpose of my view dictionary was to replace the lefthanded `cv [1,:,:] = some_floatarray` indexing with a more meaningful view indexing: `cv["red"] = some_floatarray`.

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [June 25, 2020, 4:20pm UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/7 "2020-06-25T16:20:20Z")

</div>

In my opinion,

> [@jiggunjer](#):
>
> `myview["red"] =...`

change the array (or view) stored in the Dictionary for another one. If you want to change its values, not changing it for another one (because in that case the original array is not updated) you must use `.=` as it was previouosly suggested (“.=” copy all the elements).

If it does not solve your question, I do not actually understand what you want to do.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 25, 2020, 4:31pm UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/8 "2020-06-25T16:31:16Z")

</div>

It sounds like you might be coming from C++, where doing `y = x` may actually mutate `y` by copying the contents of `x` into it. Julia doesn’t work that way–doing `y = x` just attaches the label `y` to the value of `x`, disregarding whatever value may have been previously attached to `y`. If you want to mutate the array which is currently labeled `myview["red"]`, then you want `.=` which does actually modify the array elements rather than just changing a label.

---

<div class="post-metadata">

**Author:** ![jiggunjer](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jiggunjer](https://discourse.julialang.org/u/jiggunjer)\
**Post date:** [June 26, 2020, 6:51am UTC](https://discourse.julialang.org/t/why-doesnt-this-view-assignment-work/42030/9 "2020-06-26T06:51:07Z")

</div>

@rdeits I think it clicked. I thought `cv[1,:,:]` was a label the same as `myview["red"]`.

So when I did:  
`cv[1,:,:] = some_array #assigns lhs label to a new object`  
I _should_ have done:  
`cv[1,:,:] .= some_array #overwrites object belonging to lhs label`
