简体   繁体   English

如何访问向量clojure向量中的特定元素

[英]How do I access a specific element in a vector of vectors clojure

If I have a vector defined as 如果我将矢量定义为

(def matrix [[1 2 3][4 5 6]])

How in clojure do I access a random element in a vector of vectors? 如何在clojure中访问向量向量中的随机元素? I keep seeing people say online that one of the benefits to using a vector over a list is that you get random access instead of having to recurse through a list but I haven't been able to find the function that allows me to do this. 我一直看到人们在网上说,在列表中使用向量的一个好处是你可以获得随机访问,而不必通过列表进行递归,但我无法找到允许我这样做的功能。 I'm used to in c++ where I could do matrix[1][1] and it would return the second element of the second vector. 我习惯于在c ++中我可以做矩阵[1] [1],它会返回第二个向量的第二个元素。

Am I stuck having to loop one element at a time through my vector or is there an easier way to access specific elements? 我不得不通过我的向量一次循环一个元素或者是否有更简单的方法来访问特定元素?

Vectors are associative, so you can use get-in to access nested vectors, eg matrices, by coordinates. 向量是关联的,因此您可以使用get-in通过坐标访问嵌套向量,例如矩阵。

(def matrix [[1 2 3] [4 5 6] [7 8 9]])

(get-in matrix [1 1])
;=> 5

Almost like you would do it in C++: 几乎就像你在C ++中那样:

user=> (def matrix [[1 2 3][4 5 6]])
user=> (matrix 1)
[4 5 6]
user=> ((matrix 1) 1)
5

As the docs say : 正如文档所说

Vectors implement IFn, for invoke() of one argument, which they presume is an index and look up in themselves as if by nth, ie vectors are functions of their indices. 向量实现IFn,用于一个参数的invoke(),它们假定它是一个索引并且自己查找就好像是nth,即向量是它们索引的函数。

Since vectors are associative data structures, you can also use get-in to reach inside with nested indices: 由于向量是关联数据结构,因此您还可以使用get-in通过嵌套索引到达内部:

user=> (def matrix [[1 2 3][4 5 6]])
user=> (get-in matrix [1 1])
5

The other answers are probably all that you need, but if you're doing 2D indexing a lot--perhaps along with other transformations of 2D numeric structures--you might want to look into the core.matrix library. 其他答案可能就是您需要的所有内容,但如果您正在进行大量的二维索引 - 可能还有二维数字结构的其他转换 - 您可能需要查看core.matrix库。 Switching between different core.matrix implementations with different performance characteristics is usually a one-line change. 在具有不同性能特征的不同core.matrix实现之间切换通常是单行更改。 One of the implementations consists of operations on Clojure vectors. 其中一个实现包括对Clojure向量的操作。 Here's how you would do the double-indexing in core.matrix: 以下是你在core.matrix中进行双索引的方法:

user=> (use 'clojure.core.matrix)
nil
user=> (def m (matrix [[1 2 3][4 5 6]]))
#'user/m
user=> (mget m 1 1)
5

For that particular operation, core.matrix provides no particular advantage. 对于该特定操作,core.matrix没有提供特别的优势。 If you want to iterate through the matrix to generate a new one, here's one way to do it: 如果你想迭代矩阵来生成一个新矩阵,这是一种方法:

user=> (emap inc m)
[[2 3 4] [5 6 7]]

Of course it's not very hard to do that with core Clojure functions. 当然,使用核心Clojure功能并不是很难。 Whether core.matrix is useful depends on what you want to do, obviously. core.matrix是否有用取决于你想要做什么,显然。

You can use the same approach to Mars answer above, with to-array-2d implemented in clojure.core library :) 您可以使用与上面的Mars答案相同的方法,在clojure.core库中实现to-array-2d :)

user> (def a (to-array-2d [[1 2 3][4 5 6]]))
#'user/a
user> (alength a)
2
user> (alength (aget a 0))
3
user> (aget a 0 0)
1
user> (aget a 0 1)
2
user> (aget a 0 2)
3
user> (aget a 1 0)
4
user> (aget a 2 0)
ArrayIndexOutOfBoundsException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM