# Create a 1D flat array and 2D diagonal-offset/position view on all right-left diagonals to an NxM 2D array

**URL:** https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868
**Category:** New to Julia
**Tags:** question
**Created:** [May 28, 2024, 7:00pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868 "2024-05-28T19:00:41Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 7:00pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/1 "2024-05-28T19:00:42Z")

</div>

I am struggling using Python numpy to reverse the right-left diagonals of an array and to convert them to a special diagonal items arrangement … How can this be done in Julia? Or what would be the simple formulas of obtaining the indices representing a 1D and 2D view ( in terms of diagonal index plus index of position in the diagonal ) on 2D right-left diagonals for forth and back operations?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 7:29pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/2 "2024-05-28T19:29:28Z")

</div>

[https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.diagind](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.diagind)

a la

```julia
  reverse!(@view A[diagind(A, d)])

```

See also [Linear Algebra · The Julia Language](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.diag)

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 7:44pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/3 "2024-05-28T19:44:25Z")

</div>

The detail missed in your reply seems to be that the diagonals need to be the **right to left** ones instead of the usual main left-to-right top-down diagonals.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 7:45pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/4 "2024-05-28T19:45:58Z")

</div>

Create a reversed (dims=2) view of the matrix first.

(Easier said than done?)

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 7:49pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/5 "2024-05-28T19:49:01Z")

</div>

What is a view? How to get the indices of the view in the underlying array? What does reversing an array ( reversed view) mean? I mean to know that flipping the array right to left makes it necessary to reverse the got diagonal to get it right … and writing the modified diagonal back from 1D array of stacked diagonals to the original array hard to accomplish …

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 7:52pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/6 "2024-05-28T19:52:08Z")

</div>

[https://docs.julialang.org/en/v1/base/arrays/#Views-(SubArrays-and-other-view-types)](https://docs.julialang.org/en/v1/base/arrays/#Views-(SubArrays-and-other-view-types))

```julia
julia> A=ans
3×4 reshape(::UnitRange{Int64}, 3, 4) with eltype Int64:
 1 4 7 10
 2 5 8 11
 3 6 9 12

julia> Aview = @view A[:,reverse(1:n)]
3×4 view(reshape(::UnitRange{Int64}, 3, 4), :, 4:-1:1) with eltype Int64:
 10 7 4 1
 11 8 5 2
 12 9 6 3

```

Now `Aview` is backed by `A` but appears to have its columns in reverse order.

Disregard the `reshape`. I just used that to create a comprehensible matrix.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 7:56pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/7 "2024-05-28T19:56:53Z")

</div>

Does this do what you need?

```julia
julia> A=collect(A)
3×4 Matrix{Int64}:
 1 4 7 10
 2 5 8 11
 3 6 9 12

julia> Aview = @view A[:,reverse(1:n)]
3×4 view(::Matrix{Int64}, :, 4:-1:1) with eltype Int64:
 10 7 4 1
 11 8 5 2
 12 9 6 3

julia> reverse!(@view Aview[diagind(Aview, 1)])
3-element view(reshape(view(::Matrix{Int64}, :, 4:-1:1), 12), 4:4:12) with eltype Int64:
 3
 5
 7

julia> Aview
3×4 view(::Matrix{Int64}, :, 4:-1:1) with eltype Int64:
 10 3 4 1
 11 8 5 2
 12 9 6 7

julia> A
3×4 Matrix{Int64}:
 1 4 3 10
 2 5 8 11
 7 6 9 12

```

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 7:58pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/8 "2024-05-28T19:58:39Z")

</div>

What does @view mean? What is Aview? How to get the indices of item [1,2] in the original array? What is n in 1:n? It is not defined …

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 8:03pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/9 "2024-05-28T20:03:00Z")

</div>

Can a view be created without the need of creating an actual array in first place? Just by specifying the shape/dimensions of the array?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 8:03pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/10 "2024-05-28T20:03:06Z")

</div>

@view creates a view of the array using the indices.  
[https://docs.julialang.org/en/v1/base/arrays/#Base.@view](https://docs.julialang.org/en/v1/base/arrays/#Base.@view)  
[https://docs.julialang.org/en/v1/base/arrays/#Base.view](https://docs.julialang.org/en/v1/base/arrays/#Base.view)

Sorry, n is 4, different from m, which is not shown and is the number of rows.

`1:n` is syntatic sugar for a range [Mathematics · The Julia Language](https://docs.julialang.org/en/v1/base/math/#Base.range)

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 8:04pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/11 "2024-05-28T20:04:07Z")

</div>

No, a view is an lightweight construct from an array. It makes no sense to have a view without an array.

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 8:05pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/12 "2024-05-28T20:05:35Z")

</div>

In other words I can’t get the target coordinates in the underlying array out of the view? What I am looking for is a way to obtain the transform function from one system of indices to another … this does not need an actual array which is only required if I want to get values out of it. Such “general view” could allow access to values of any array with same shape not being bound to an existing array with values …

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 8:09pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/13 "2024-05-28T20:09:20Z")

</div>

It does appear that one can `@view` a range, which is an `AbstractArray`. I’m unaware of a method for `diagind` that works with just the dimensions of the array, or an implementation of `AbstractArray` that looks like a 2-dimensional view.

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 8:14pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/14 "2024-05-28T20:14:01Z")

</div>

In other words it is necessary for my purpose to define/create own functions which when fed with indices specifying the right-left diagonal offset and the index of item in it calculate the indices to the target array with for example reversed diagonals? How would such function look like for the example of specifying x,y returning xTarget, yTarget for obtaining a value which would be of an array with reversed anti-diagonals ?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 8:17pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/15 "2024-05-28T20:17:27Z")

</div>

You might find something useful in the source for `diagind` (link below), but it relies on a Julia matrix being indexable with a single index. If you were to create your function in Julia, you could do that with the snippets from above if you implement a lazy `AbstractArray` whose values are the flat array indices, ie., like `A` above.

> <https://github.com/JuliaLang/julia/blob/master/stdlib/LinearAlgebra/src/dense.jl#L208-L253>

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 8:22pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/16 "2024-05-28T20:22:59Z")

</div>

OK … so it is possible to index any multi-dimensional array by a single index without flatting it in first place? This means there is a native view on the flat array for each array? How to get the flat index out of a view on an array given the multi-dimensional indices?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 8:35pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/17 "2024-05-28T20:35:47Z")

</div>

```julia
julia> typeof(Aview)
SubArray{Int64, 2, Matrix{Int64}, Tuple{Base.Slice{Base.OneTo{Int64}}, StepRange{Int64, Int64}}, false}

```

`SubArray` is the structure for a view of an array. It contains the information for the view index mapping.

[https://docs.julialang.org/en/v1/devdocs/subarrays/#SubArray-design](https://docs.julialang.org/en/v1/devdocs/subarrays/#SubArray-design)

> This means there is a native view on the flat array for each array?

Not really an explicit view, it’s just part of the implementation of an array to support linear indexing.

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 10:17pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/18 "2024-05-28T22:17:25Z")

</div>

By the way: do you know how it comes that Julia went the path of one-based array indices? What was the main reason for such design choice?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 28, 2024, 10:22pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/19 "2024-05-28T22:22:06Z")

</div>

> <https://github.com/julialang/julia/issues/558#issuecomment-4428498>
>
> For most language they are start with 0. Why Julia choose zero. 
> And from mathem…atical point of view, start with 0 is better.
> 
> 1 1 Java 17.050% -1.43% A
> 2 2 C 16.523% +1.54% A
> 3 6 C# 8.653% +1.84% A
> 4 3 C++ 7.853% -0.33% A
> 5 8 Objective-C 7.062% +4.49% A
> 6 5 PHP 5.641% -1.33% A
> 7 7 (Visual) Basic 4.315% -0.61% A
> 8 4 Python 3.148% -3.89% A
> 9 10 Perl 2.931% +1.02% A
> 10 9 JavaScript 2.465% -0.09% A

Bezanson said, “We have chosen 1 to be more similar to existing math software”

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 28, 2024, 10:39pm UTC](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868/20 "2024-05-28T22:39:37Z")

</div>

OK … this seems to be a software oriented decision … I for myself and from the perspective of the oOo-system (see github and dev.to for more if interested) consider zero based indexing what screws your mind into counting apples in the basket starting with zero - what makes absolutely no sense … any street and any postal code starts from one … this is sane … not harming the mind with concepts far from reality putting screwed labels on items in a container … I suppose that the insanity which begun with C propagated itself over the history of programming languages. Nice to see that there is an option to this screwed way driving your mind crazy … By the way: WHO is _Bezanson_ ?

[Next page](https://discourse.julialang.org/t/create-a-1d-flat-array-and-2d-diagonal-offset-position-view-on-all-right-left-diagonals-to-an-nxm-2d-array/114868.md?page=2)
