如何在Julia中返回Objecr数组元素的索引 [英] How to return index of Objecr array elements in Julia

查看:173
本文介绍了如何在Julia中返回Objecr数组元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试返回Vector集合中选定元素的索引

i trying return index of selected element of my Vector collection

type Node
       name::AbstractString
       value::Int
       left::Nullable{Node}
       right::Nullable{Node}

       Node(name::AbstractString, value::Int) = new(name, value, Nullable{Node}(), Nullable{Node}())
end

function minimal(nodes::Vector{Node})
                minnode=Nullable{Node}()
                minval = nodes[1].value
                    for f in nodes
                        if f.value< minval
                            minval= f.value
                            minnode = f
                        end
                    end
                return find(nodes .== minnode)
end

问题当然是find(nodes .== minnode),如何返回该元素的索引

Problem is of course find(nodes .== minnode), how can I return index of this element

推荐答案

该函数似乎过大,这样做是否足够:

The function seems to be overkill, wouldn't it be sufficient to do:

findmin([i.value for i in nodes])[2]

例如

nodes = [Node("a",12), Node("b",4),Node("c",-5)]

这将返回3,即节点列表中最小节点的索引.

this returns 3, the index of the smallest node in the node list.

这篇关于如何在Julia中返回Objecr数组元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆