Question about findmax

this is my code:

using StatsBase,DelimitedFiles
#function Characteristics(Data,)
    global max_index_mean_var=Matrix{Matrix{Float64}}(undef,4*size(Data,1),size(Data,2))
#    global mean_var=Matrix{Matrix{Float64}}(undef,2*size(Data,1),size(Data,2))
    for i=1:size(Data,1)
        j=1:size(Data,2)
        #max_index_mean_var[4*i-3,j]=maximum(Data[i,j],dims=1)
        max_index_mean_var[4*i-3,j]=getindex.(findmax(Data[i,j],dims=1)[1],1)
        #max_index_mean_var[4*i-2,j]=getindex.(getindex(findmax(Data[i,j][1],dims=1),1))
        #max_index_mean_var[4*i-1,j]=mean_and_var(Data[i,j],2)[1]
        #max_index_mean_var[4*i,j]=mean_and_var(Data[i,j],2)[2]
    end

this is the "Data"in my code :


this is the error:
ERROR: LoadError: MethodError: no method matching isless(::Array{Float64,2}, ::Array{Float64,2})
Closest candidates are:
isless(::Missing, ::Any) at missing.jl:87
isless(::Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R, ::Any) at C:\Users\27990.juliapro\JuliaPro_v1.4.2-1\packages\CategoricalArrays\dmrjI\src\value.jl:191
isless(::Any, ::Missing) at missing.jl:88
I tried to fix the code, but nothing worked,I don’t think this code is logically flawed,why???
please help me!thanks!!!

This error here

indicates that Data[i, j] is not a matrix, but an array of matrices. That means that Data is an array of arrays of arrays, instead of just an array of arrays. Can you try to figure that out? What are

typeof(Data)

and

typeof(Data[1,1])

?

You cannot call findmax on an array of arrays, since there is no defined method for determining if one array is greater than another (that’s the missing isless method.)

global a=Matrix{Matrix{Float64}}(undef,5,2)
q=Matrix{Matrix{Float64}}(undef,5,2)
b=[1 22 13;11 5 6;7 8 9]
for i=1:10
    j=1:2
global a[i]=b
end

for i=1:5
    for j=1:2
q[i,j]=getindex.(findmax(a[i,j],dims=1)[1],1)
end 
end

But this code works,why?

It works because a[i, j] is an ordinary matrix here, but that is not the case for Data[i, j], apparently.

By the way, it is needlessly complicated to write

q[i,j]=getindex.(findmax(a[i,j],dims=1)[1],1)

Instead, just write

q[i, j] = maximum(a[i, j]; dims=1)


They’re of the same type

Can you run the following code:

typeof(Data)

And then

maximum(Data[1, 1]; dims=1)

?
(Sorry, I had a typo at first)

What happens if you run

global max_index_mean_var=Matrix{Matrix{Float64}}(undef,4*size(Data,1),size(Data,2))
#    global mean_var=Matrix{Matrix{Float64}}(undef,2*size(Data,1),size(Data,2))
    for i=1:size(Data,1)
        j=1:size(Data,2)
        #max_index_mean_var[4*i-3,j]=maximum(Data[i,j],dims=1)
        max_index_mean_var[4*i-3,j]=getindex.(findmax(Data[i,j],dims=1)[1],1)
        #max_index_mean_var[4*i-2,j]=getindex.(getindex(findmax(Data[i,j][1],dims=1),1))
        #max_index_mean_var[4*i-1,j]=mean_and_var(Data[i,j],2)[1]
        #max_index_mean_var[4*i,j]=mean_and_var(Data[i,j],2)[2]
    end

now?

By the way, you should really avoid using globals, and make sure that your code are functions.


it always does not work

I’m afraid I cannot see what’s wrong here.

What is the output of:
q=Matrix{Any}(undef,size(Data,1),size(Data,2));
for i=1:size(Data,1)
    for j=1:size(Data,2)
       q[i,j]=typeof(Data[i,j])
    end
end
unique(q)
I hope it works, but the idea is clear? Output should be:
julia> unique(q)
1-element Array{Any,1}:
 Array{Float64,2}

thank you anyway!

It’s supposed to be the error of the findmax function, but I’ve done other experiments, and everything else is correct except this one :persevere:

I suspect your Data structure has some wrong entries. Thats the test for to check if every single entry is of type ``` Array{Float64,2} ```

I see my error now :slight_smile: