harang
harang

Reputation: 11

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

Can I get the average of multidimensional matrix data in the 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
]

If I have a matrix file, can I get the average of all the data in the same place in the matrix file? In the same row and column?

And also, I cannot use any statistics method in my data. (using Statistics)

Here is my code.

julia> using BioStructures

julia> struc0=read("ranked_0.pdb",PDB)
ProteinStructure ranked_0.pdb with 1 models, 1 chains (A), 398 residues, 6267 atoms

julia> struc1=read("ranked_1.pdb",PDB)
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]


julia> 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 between 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
julia> dm0=DistanceMap(calphas0)
Distance map of size (398, 398)
julia> 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 mean function doesn't work at all.

Upvotes: 1

Views: 277

Answers (1)

Shayan
Shayan

Reputation: 6295

You can do this:

julia> foo = [
       1 2 3
       4 5 6
       ]
2×3 Matrix{Int64}:
 1  2  3
 4  5  6

julia> bar = [
       8 2 7
       9 6 3
       ]
2×3 Matrix{Int64}:
 8  2  7
 9  6  3

julia> (foo+bar)/2
2×3 Matrix{Float64}:
 4.5  2.0  5.0
 6.5  5.5  4.5

In your case, In the last phase of the DistanceMap function, you're returning the function object, which isn't what you want to do apparently. So the last line of the function should be replaced with the return dists:

function DistanceMap(el::StructuralElementOrList)
    dists = zeros(length(el), length(el))
    # do jobs...
    return dists
end

function DistanceMap(el1::StructuralElementOrList, el2::StructuralElementOrList)
    dists = zeros(length(el1), length(el2))
    # do jobs...
    return dists
end

Then, the (dm0+dm1)/2 will give you what you expect.


Using the mean function with specified dimension (specifically related to Statistics.jl package)
It seems to me you don't know how the mean(dm0,dims=1) works. Say I have this simple 2x3 matrix:

julia> foo = [
       1 2 3
       4 5 6
       ]
2×3 Matrix{Int64}:
 1  2  3
 4  5  6

We want to observe how the mean(foo, dims=1) works in this case. First, I should mention that performing the mean function on a collection of values calculates the mean of all the elements:

julia> mean(foo)
3.5

But what happens when we specify the dimension we want to take the mean? In this case, mean calculate the average value in the given matrix over a dimension. For example, If I want to take the mean of foo over the first dimension (each column), the following shows what is going to happen:

julia> mean(foo, dims=1)
1×3 Matrix{Float64}:
 2.5  3.5  4.5

enter image description here

Upvotes: 2

Related Questions