How can I get average of the multidimensional matrix datas in Julia?

Can I get average of multidimensional matrix datas in same place in Julia?
such as…
A={2 4 6
8 10 12}
B={4 6 8
10 12 14}

Average={3 5 7
9 11 13}

However I cannot use any statistics method in my data.(using Statistics)

``````Here is my code.

> using BioStructures
ProteinStructure ranked_0.pdb with 1 models, 1 chains (A), 398 residues, 6267 atoms
ProteinStructure ranked_1.pdb with 1 models, 1 chains (A), 398 residues, 6267 atoms

#PDB is protein structure file
#calphaselector collects alpha-carbon in protein structure

julia>calphas1=collectatoms(struc1['A'],calphaselector)
398-element Vector{AbstractAtom}:
Atom CA with serial 5, coordinates [-48.92, 44.228, -7.122]
Atom CA with serial 22, coordinates [-45.297, 43.116, -7.895]
Atom CA with serial 44, coordinates [-44.734, 41.122, -11.106]
Atom CA with serial 68, coordinates [-43.19, 37.683, -11.734]
Atom CA with serial 90, coordinates [-40.072, 37.975, -13.951]
Atom CA with serial 109, coordinates [-39.962, 34.961, -16.27]
Atom CA with serial 120, coordinates [-36.574, 35.287, -18.039]
Atom CA with serial 142, coordinates [-36.609, 33.35, -21.312]
Atom CA with serial 152, coordinates [-34.149, 30.463, -21.787]
Atom CA with serial 171, coordinates [-32.469, 30.904, -25.188]
Atom CA with serial 190, coordinates [-32.407, 27.558, -27.034]
Atom CA with serial 201, coordinates [-28.96, 27.662, -28.654]
Atom CA with serial 211, coordinates [-28.549, 24.452, -30.698]
⋮
Atom CA with serial 6078, coordinates [-24.236, -1.604, 12.74]
Atom CA with serial 6093, coordinates [-26.218, 1.577, 11.906]
Atom CA with serial 6117, coordinates [-24.924, 4.783, 13.587]
Atom CA with serial 6136, coordinates [-27.904, 7.205, 13.764]
Atom CA with serial 6147, coordinates [-28.211, 10.904, 14.654]
Atom CA with serial 6154, coordinates [-25.655, 12.413, 17.087]
Atom CA with serial 6168, coordinates [-24.017, 9.206, 18.385]
Atom CA with serial 6179, coordinates [-20.62, 7.793, 19.378]
Atom CA with serial 6193, coordinates [-19.557, 4.234, 18.472]
Atom CA with serial 6214, coordinates [-16.767, 2.846, 20.702]
Atom CA with serial 6228, coordinates [-14.132, 0.846, 18.748]
Atom CA with serial 6247, coordinates [-11.916, 0.05, 21.82]
> calphas0=collectatoms(struc0['A'],calphaselector)
398-element Vector{AbstractAtom}:
Atom CA with serial 5, coordinates [-36.089, 66.106, 25.299]
Atom CA with serial 22, coordinates [-33.821, 64.881, 22.365]
Atom CA with serial 44, coordinates [-33.557, 62.444, 20.056]
Atom CA with serial 68, coordinates [-32.879, 58.931, 18.504]
Atom CA with serial 90, coordinates [-29.619, 58.508, 16.472]
Atom CA with serial 109, coordinates [-30.364, 57.074, 13.005]
Atom CA with serial 120, coordinates [-28.582, 54.105, 11.366]
Atom CA with serial 142, coordinates [-25.95, 54.934, 8.729]
Atom CA with serial 152, coordinates [-25.746, 52.003, 6.279]
Atom CA with serial 171, coordinates [-22.186, 52.037, 4.888]
Atom CA with serial 190, coordinates [-22.536, 50.445, 1.448]
Atom CA with serial 201, coordinates [-19.07, 48.955, 0.906]
Atom CA with serial 211, coordinates [-19.304, 47.721, -2.701]
⋮
Atom CA with serial 6078, coordinates [-30.501, 3.783, 9.927]
Atom CA with serial 6093, coordinates [-30.924, 7.529, 10.661]
Atom CA with serial 6117, coordinates [-28.57, 8.891, 13.374]
Atom CA with serial 6136, coordinates [-30.304, 12.0, 14.818]
Atom CA with serial 6147, coordinates [-29.161, 14.746, 17.23]
Atom CA with serial 6154, coordinates [-26.384, 13.957, 19.761]
Atom CA with serial 6168, coordinates [-26.4, 10.132, 19.458]
Atom CA with serial 6179, coordinates [-23.963, 7.201, 19.425]
Atom CA with serial 6193, coordinates [-24.374, 4.256, 17.037]
Atom CA with serial 6214, coordinates [-22.595, 1.092, 18.252]
Atom CA with serial 6228, coordinates [-20.864, -0.796, 15.39]
Atom CA with serial 6247, coordinates [-19.446, -3.617, 17.623]
# calculate Distance betweeen alpha-carbon

function DistanceMap(el1::StructuralElementOrList,
el2::StructuralElementOrList)
dists = zeros(length(el1), length(el2))
for (i, subel1) in enumerate(el1)
for (j, subel2) in enumerate(el2)
dists[i, j] = distance(subel1, subel2)
end
end
return DistanceMap(dists)
end

function DistanceMap(el::StructuralElementOrList)
dists = zeros(length(el), length(el))
el_list = collect(el)
for i in 1:length(el)
for j in 1:(i - 1)
dist = distance(el_list[i], el_list[j])
dists[i, j] = dist
dists[j, i] = dist
end
end
return DistanceMap(dists)
end
``````

dm0=DistanceMap(calphas0)
Distance map of size (398, 398)
dm1=DistanceMap(calphas1)
Distance map of size (398, 398)

#Here is some Distance Map Data
julia> dm0[1,3]
6.87824955929922

julia> dm1[1,1]
0.0

julia> dm1[1,3]
6.560646919321296

julia> println(typeof(size(dm0)))
Tuple{Int64, Int64}

julia>using Statistics

julia> mean(dm0,dims=1)
ERROR: MethodError: no method matching mean(::DistanceMap; dims=1)
Closest candidates are:
mean(::Any) at /builddir/build/BUILD/julia-1.6.5/build/usr/share/julia/stdlib/v1.6/Statistics/src/Statistics.jl:44 got unsupported keyword argument “dims”
mean(::Any, ::AbstractArray; dims) at /builddir/build/BUILD/julia-1.6.5/build/usr/share/julia/stdlib/v1.6/Statistics/src/Statistics.jl:104
mean(::Any, ::Any) at /builddir/build/BUILD/julia-1.6.5/build/usr/share/julia/stdlib/v1.6/Statistics/src/Statistics.jl:61 got unsupported keyword argument “dims”

Stacktrace:
[1] top-level scope
@ REPL[19]:1

``````
#I don't know the reason that the function 'mean' doesn't work at all.
``````

I’m not familiar with `DistanceMap`s, but this is fairly straightforward:

``````julia> A = [2 4 6; 8 10 12]
2×3 Matrix{Int64}:
2   4   6
8  10  12

julia> B = [4 6 8; 10 12 14]
2×3 Matrix{Int64}:
4   6   8
10  12  14

julia> [(a+b)/2 for (a,b) ∈ zip(A,B)]
2×3 Matrix{Float64}:
3.0   5.0   7.0
9.0  11.0  13.0
``````

I think this doesn’t work because DistanceMap data is not a such form I expected.

[(a+b)/2 for (a,b) ∈ zip(dm0,dm1)]
ERROR: MethodError: no method matching length(::DistanceMap)

I think I have to array it first. Thank you!

1 Like

From a brief look at BioStructures.jl, it appears that the relevant data will be stored in array form in `dm0.data` and `dm1.data`.

1 Like

Note: property access, especially on properties that are not officially exposed as part of the object’s user interface, is not considered idiomatic Julia. It would be preferred if the relevant iteration utilities for the `DistanceMap` type were defined, so that we could simply write `[(a+b)/2 for (a,b) ∈ zip(A,B)]`. It could be worth contacting the package authors, or contributing to the project yourself, to resolve this problem.