I have found Julia’s comprehensions to be one of its most useful features, but I am running into a problem which I hope someone can help me out with.
Let’s say I have an array of size N x 10 in a variable named data
, where N is very large. It looks like this:
No. | Time | dt | Fx | Fy |
---|---|---|---|---|
0 | 0 | 0.00454545 | 0 | 0 |
1 | 0.00454545 | 0.00454545 | -0.889309 | 0.016332 |
… | … | … | … | … |
Then, using the following one-liner, I can extract those values of Fx
and Fy
for which the time obeys certain conditions, e.g., that it be greater than 100:
[data[i,4:5] for i in 1:length(data[:,1]) if data[i,2] > 100]
However, this gives me an array of arrays instead of a n x 2 array:
[1.49301, 1.12155]
[1.49276, 1.12132]
[1.49267, 1.12127]
...
The problem with this is that I would then like to average both Fx
and Fx
along the time dimension. And the formulation mean(A,dims=1)
requires A to be a 2D array, not a 1D array of arrays. So what I’d like is to be able to do something like this:
[mean(data[i,4:5],dims=1) for i in 1:length(data[:,1]) if data[i,2] > 100]
and to then get a single 1 x 2 array contianing the averages.
I understand that I can easily do this sort of calculation by writing a few more lines of code. But Julia is so close to giving me a single, elegant line of code that will do this calculation in one go! Can anyone help me figure out how to make the output of a comprehension like this be a n x 2 array instead of an array of arrays?