Hi there! I am a bit new to julia so I have a few a very basic questions:

I want to sum 33 rows of an nxm matrix. How can I do so?

Hi there! I am a bit new to julia so I have a few a very basic questions:

I want to sum 33 rows of an nxm matrix. How can I do so?

Hi @r.bhl, welcome to the community! Your question is really about array indexing. Have a look at the manual here (you can skip straight to the indexing section if you wish):

and let us know if it’s still unclear or if you need further assistance.

Hello @mthelm85! thank you for the reply!

The question I have to answer: sort the 30 assets according to the sum of returns over the last 33 days. Assets are given in columns and days are the rows. Data is given for 10 years, and I have to (I think) create a loop for returns.

**My commands I have tried:**

#R is the return which I indexed from a larger database.

m = 1

n = R[1:30]

for t = [1:33][1]

local n

m = m + t

n = t

R2 = sum(R[m])

end

printmat(R2)

**Another command I tried:**

Rsum = sum(R,dims=1)

How could I go about this?

You should always quote your code here with backticks ```, like this:

```
m = 1
n = R[1:30]
for t = [1:33][1]
local n
m = m + t
n = t
R2 = sum(R[m])
end
printmat(R2)
```

It makes it much easier to read Have a look at this thread when you get a chance.

I’m not sure I’m following but if you have some returns data in a `Matrix`

R and then the names of the assets in a vector `assets`

, you can do this:

```
# Create fake data
R = rand(Float64, (50,30))
assets = ["company$i" for i in 1:30]
rets = [(company=i[2], ret=sum(i[1][1:33])) for i in zip(eachcol(R), assets)]
julia> sorted_assets = [sort(rets, by=last, rev=true)[i][1] for i in 1:30]
30-element Array{String,1}:
"company18"
"company11"
"company6"
"company29"
"company7"
"company5"
"company1"
"company30"
⋮
"company15"
"company3"
"company26"
"company21"
"company20"
"company2"
"company22"
"company14"
```

That being said, I would probably use DataFrames.jl to do this kind of work. The documentation for that package is good and it will probably allow you to get up and running with these kinds of operations faster than if you just use base Julia.

2 Likes

Thank you so much @mthelm85. Just a small doubt- the array we get of the 30 element string is a sorted function of their summed returns? If not, how could I rank these assets on the basis of their sum?

I can’t thank you enough. I am new to this language and amidst covid it’s become even tougher to seek guidance.

1 Like

Anytime! You’ll find that the community here is very helpful and on numerous occasions, I’ve been amazed at how much time & effort folks are willing to put in to help me out (so I do my best to pay it forward ). If you can provide a minimum working example (MWE), you’ll nearly always receive help that will allow you to solve your problem .

In the example above, if I just wanted an array of sorted returns, I would do something like this (this is much easier):

```
R = rand(Float64, (50,30))
julia> rets = sort([sum(col[1:33]) for col in eachcol(R)], rev=true)
30-element Array{Float64,1}:
20.547714995916056
20.0038231068496
19.6576605056632
19.412501859347227
19.170970130142976
18.272165423676235
18.25366634943681
17.822982398212552
17.76791261284992
⋮
15.134177106423126
15.098637341222217
14.878716664527401
14.833317877379049
14.486749764568177
14.455917909240188
14.183262764805674
14.117427894274988
```

To fully understand the above, check out the following sections of the manual:

Also, note that you can type `?`

in the REPL and then search for function names, types, keywords, etc. So, if you want to understand the `sort`

, `rand`

and `eachcol`

functions, in your Julia REPL, just type `?`

and you’ll see it change from `julia>`

to `help?>`

. Then, you’ll see something like this:

```
help?> rand
search: rand randn transcode macroexpand @macroexpand @macroexpand1 CartesianIndex CartesianIndices range transpose LinRange UnitRange StepRange
rand([rng=GLOBAL_RNG], [S], [dims...])
.
. # more info here
.
```

2 Likes