# Exceptions differentiated by the length of an array

**URL:** <https://discourse.julialang.org/t/exceptions-differentiated-by-the-length-of-an-array/94306>\
**Category:** New to Julia\
**Tags:** question, array\
**Created:** [February 8, 2023, 9:34pm UTC](https://discourse.julialang.org/t/exceptions-differentiated-by-the-length-of-an-array/94306 "2023-02-08T21:34:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matilopez06](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matilopez06/32/46480_2.png) [@Matilopez06](https://discourse.julialang.org/u/Matilopez06)\
**Post date:** [February 8, 2023, 9:34pm UTC](https://discourse.julialang.org/t/exceptions-differentiated-by-the-length-of-an-array/94306/1 "2023-02-08T21:34:53Z")

</div>

Hello everyone, I wanted to ask a question.  
I am currently getting an array of doubles containing matrix = [magnitudes ; angles] where  
I have different cases (separated by spaces and “;”):

case 1: [X1 X2; angle\_x1 angle\_x2] … [2x2]  
case 2: [X1 Y1 X2 Y2; angle\_x1 angle\_y1 angle\_x2 angle\_y2] … [2x4]  
case 3: [X1 Y1 Z1 X2 Y2 Z2; angle\_x1 angle\_y1 angle\_z1 angle\_x2 angle\_y2 angle\_z2] … [2x6]

I would like to be able to make exceptions according to the size of my array (or according to its number of columns) to retrieve information, for example if I have case 1 where the size of the matrix is M1, I only retrieve X1, if case 2 where my matrix is of size M2 get X1 and Y2, if case 3 where the size of the matrix is M3 get x1 y1 z1.

thx to all.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 8, 2023, 9:42pm UTC](https://discourse.julialang.org/t/exceptions-differentiated-by-the-length-of-an-array/94306/2 "2023-02-08T21:42:12Z")

</div>

just use if-else, naively Matrix all have the same type information (unless you do some hackery to propagate this information but it’s probably not worth it)

---

<div class="post-metadata">

**Author:** ![Matilopez06](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matilopez06/32/46480_2.png) [@Matilopez06](https://discourse.julialang.org/u/Matilopez06)\
**Post date:** [February 8, 2023, 10:24pm UTC](https://discourse.julialang.org/t/exceptions-differentiated-by-the-length-of-an-array/94306/3 "2023-02-08T22:24:31Z")

</div>

Hello! Can you give me an example?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [February 9, 2023, 2:16am UTC](https://discourse.julialang.org/t/exceptions-differentiated-by-the-length-of-an-array/94306/4 "2023-02-09T02:16:06Z")

</div>

```julia
function magnitudes(matrix)
    size(matrix, 2) == 2 && return matrix[1, 1]
    size(matrix, 2) == 4 && return matrix[1, [1, 4]]
    size(matrix, 2) == 6 && return matrix[1, 1:3]
    error("Expected matrix of width 2, 4 or 6, got $(size(matrix, 2))")
end

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 9, 2023, 2:39am UTC](https://discourse.julialang.org/t/exceptions-differentiated-by-the-length-of-an-array/94306/5 "2023-02-09T02:39:15Z")

</div>

You might want to have these cases as different types, and then dispatch on the appropriate type.  
For example in this case, explicit types for `1D` `2D` or `3D`, or a single type parametrized by `D`, as in `Point{D}`.  
This could pay for itself in future with efficiency when this `D` is known at compile type.
