Question about Variable declarations

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?

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.

1 Like

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

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

Generally, in Julia you do not need to declare variables.

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})

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

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

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.

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

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

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.

1 Like

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:

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

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.

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