# Question about Variable declarations

**URL:** <https://discourse.julialang.org/t/question-about-variable-declarations/44845>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 13, 2020, 2:26am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845 "2020-08-13T02:26:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 13, 2020, 2:26am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/1 "2020-08-13T02:26:21Z")

</div>

In the writing process of Julia function, we need to input n as the parameter,that is `function abc(data,f,n,...)`.I have a data matrix, and I’m going to divide the data matrix into n parts according to index f,How do I declare n matrices and then block them by the index?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 13, 2020, 2:34am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/2 "2020-08-13T02:34:21Z")

</div>

> [@abcde](#):
>
> How do I declare n matrices and then block them by the index?

What do you mean by “block them by the index”? I am not sure if you are providing sufficient information about what you want to achieve to allow us to understand the problem.

---

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 13, 2020, 2:44am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/3 "2020-08-13T02:44:50Z")

</div>

That is, the index Matrix f containing the index vector,Each column of f contains the different index values,Then divide the data according to the index value of each column in f

---

<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:** [August 13, 2020, 7:35am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/4 "2020-08-13T07:35:55Z")

</div>

To second @Henrique_Becker, what you are trying to do is still unclear.

Generally, in Julia you do not need to [declare variables](https://en.wikipedia.org/wiki/Variable_declaration).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 13, 2020, 12:27pm UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/5 "2020-08-13T12:27:10Z")

</div>

…

> [@abcde](#):
>
> That is, the index Matrix f containing the index vector

Ok, `f` is then a matrix of vectors of indexes? Like `f :: Array{Vector{Int}, 2}`? Something like that? Or did you `vector` wrong and you just meant a matrix of ints? (`f :: Array{Int, 2}`)

> [@abcde](#):
>
> Each column of f contains the different index values

Here it seems like it is just a matrix of Ints.

> [@abcde](#):
>
> Then divide the data according to the index value of each column in f

Let me just check if I understood, you want something like that?

```julia
data = [1 2 3; 4 5 6; 7 8 9]
f = [1 1 2; 2 2 3; 3 3 1]
result = abc(data, f, 3, ...)
result == (
    [1 2 0; 0 0 0; 0 0 9],
    [0 0 3; 4 5 0; 0 0 0],
    [0 0 0; 0 0 6; 7 8 0],
)

```

Or `missing` instead of zero, I do not know.

---

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 14, 2020, 6:45am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/6 "2020-08-14T06:45:21Z")

</div>

Yes, but f needs input, so I don’t know how to declare an unknown number of matrices

---

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 14, 2020, 6:48am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/7 "2020-08-14T06:48:15Z")

</div>

I need to apply a loop to deal with these matrices, so it’s necessary to define a new matrix

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 14, 2020, 9:51am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/8 "2020-08-14T09:51:55Z")

</div>

As already mentioned, it’s not at all clear what you want. Was the case posted above what you’re looking for? If not, it would help if you wrote a set of example inputs and the desired output yourself.

---

<div class="post-metadata">

**Author:** ![arik](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@arik](https://discourse.julialang.org/u/arik)\
**Post date:** [August 14, 2020, 11:09am UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/9 "2020-08-14T11:09:43Z")

</div>

> Yes, but f needs input, so I don’t know how to declare an unknown number of matrices

Not quite sure what you mean too, but, in general:

-If you want to pass an arbitrary number of Matrices to a function, use the `...`-operator, like in  
`abc(f::Array,n::Integer,data::Matrix...)`  
which will allow any number of Matrices to be passed to `abc(...)` after `f` and `n`

-If you want to return an unknown number of matrices, I recommend returning a Vector of Matrices, example:

```julia
thisManyMatrices=4
returnArray=Vector{Matrix{Float64}}(undef,thisManyMatrices)
for i in eachindex(returnArray)
    returnArray[i]=....
end
return returnArray

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 14, 2020, 12:08pm UTC](https://discourse.julialang.org/t/question-about-variable-declarations/44845/10 "2020-08-14T12:08:20Z")

</div>

> [@abcde](#):
>
> f needs input, so I don’t know how to declare an unknown number of matrices

Input or output? Do you mean the `data` argument will be many matrices? Then just use a `Vector` of matrices and pass it as `data`. Or you mean that `abc` will return many matrices and you need to declare them inside `abc`? If that is the case, compute the number of matrices you need to return and create a `Vector` of the right size to store them, fill it by a loop, and return this new `Vector`.

> [@abcde](#):
>
> I need to apply a loop to deal with these matrices, so it’s necessary to define a new matrix

I am not sure how one things leads to the other.
