# A usage about multi-dimension arrays

**URL:** <https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 20, 2018, 4:02am UTC](https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548 "2018-10-20T04:02:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lannisite110](https://avatars.discourse-cdn.com/v4/letter/l/df705f/32.png) [@lannisite110](https://discourse.julialang.org/u/lannisite110)\
**Post date:** [October 20, 2018, 4:02am UTC](https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548/1 "2018-10-20T04:02:20Z")

</div>

i try to find that how to using multi-dimension arrays minus a constant in julia1.0 documents, but i fail. so can you help me? thanks

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 20, 2018, 6:17am UTC](https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548/2 "2018-10-20T06:17:00Z")

</div>

> [@lannisite110](#):
>
> multi-dimension arrays minus a constant

Do you need to subtract a scalar from each element? Then use [broadcasting](https://docs.julialang.org/en/stable/manual/mathematical-operations/#man-dot-operators-1), eg

```julia
some_array .- some_scalar

```

If you need something else, please explain in detail (eg provide an example of the desired input and output).

---

<div class="post-metadata">

**Author:** ![lannisite110](https://avatars.discourse-cdn.com/v4/letter/l/df705f/32.png) [@lannisite110](https://discourse.julialang.org/u/lannisite110)\
**Post date:** [October 20, 2018, 7:45am UTC](https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548/3 "2018-10-20T07:45:44Z")

</div>

xtrn[:,:,1,:] = xtrn[:,:,1,:].-rxtrn;  
where xtrn is a 4 dim arrays and rxtrn is a constant.  
when i run the file in this line, the peoplem as following occured.  
ERROR: LoadError: syntax: extra token “rxtrn” after end of expression

---

<div class="post-metadata">

**Author:** ![lannisite110](https://avatars.discourse-cdn.com/v4/letter/l/df705f/32.png) [@lannisite110](https://discourse.julialang.org/u/lannisite110)\
**Post date:** [October 20, 2018, 8:00am UTC](https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548/4 "2018-10-20T08:00:45Z")

</div>

there occur a strange thing. when i change xtrn[:,:,1,:] = xtrn[:,:,1,:].-rxtrn to xtrn[:,:,1,:] = xtrn[:,:,1,:].-rxtrn;xtr=xtrn[:,:,1,:].-rxtrn;xtrn[:,:,1,:]=xtr. it work. And the last sentence have no need to change, do you know the reason. I want to know that.

> [@Tamas\_Papp](#):
>
> > [@lannisite110](#):
> >
> > multi-dimension arrays minus a constant
> 
> Do you need to subtract a scalar from each element? Then use [broadcasting](https://docs.julialang.org/en/stable/manual/mathematical-operations/#man-dot-operators-1), eg
> 
> ```julia
> some_array .- some_scalar
> 
> ```
> 
> If you need something else, please explain in detail (eg provide an example of the desired input and output).

```julia
    xtrn[:,:,1,:] = xtrn[:,:,1,:].-rxtrn;
    xtr=xtrn[:,:,1,:].-rxtrn
    xtrn[:,:,1,:]=xtr;
    xtrn[:,:,2,:] = xtrn[:,:,2,:].-gxtrn;
    xtrn[:,:,3,:] = xtrn[:,:,3,:].-bxtrn;

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 20, 2018, 8:39am UTC](https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548/5 "2018-10-20T08:39:27Z")

</div>

Please post an MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [October 20, 2018, 9:21am UTC](https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548/6 "2018-10-20T09:21:43Z")

</div>

It may need a `.=` with an indexing operation ( `a[]` ) on the left, and with all(?) broadcast-like ( `.-` ) operators on the right, for the broadcast fusion optimization

```julia
a = ones(3,3,3,3);
a[:,:,1,:] .= a[:,:,1,:] .- 1;

b = ones(3,2); c = ones(2, 1);
b[:, 1] = b[:,1] * c[1,1] ##matrix multiply
```

---

<div class="post-metadata">

**Author:** ![lannisite110](https://avatars.discourse-cdn.com/v4/letter/l/df705f/32.png) [@lannisite110](https://discourse.julialang.org/u/lannisite110)\
**Post date:** [October 20, 2018, 11:55am UTC](https://discourse.julialang.org/t/a-usage-about-multi-dimension-arrays/16548/7 "2018-10-20T11:55:02Z")

</div>

thank you. I solved it.
