Julia中的多维数组理解 [英] Multidimensional Array Comprehension in Julia

查看:106
本文介绍了Julia中的多维数组理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想念Julia,似乎无法使多维数组理解工作.我正在为OSX使用每晚0.20-pre的版本;可以想象这可能是构建中的错误.但是,我怀疑这是用户的错误.

I'm mucking about with Julia and can't seem to get multidimensional array comprehensions to work. I'm using a nightly build of 0.20-pre for OSX; this could conceivably be a bug in the build. I suspect, however, it's a bug in the user.

让我说我想结束类似的事情:

Lets say I want to wind up with something like:

5x2 Array
1 6
2 7
3 8
4 9
5 10

我不想只打电话给reshape.据我所知,应该生成一个多维数组,例如:[(x, y) for x in 1:5, y in 6:10].但这会生成一个5x5的元组数组:

And I don't want to just call reshape. From what I can tell, a multidimensional array should be generated something like: [(x, y) for x in 1:5, y in 6:10]. But this generates a 5x5 Array of tuples:

julia> [(x, y) for x in 1:5, y in 6:10]
5x5 Array{(Int64,Int64),2}:
 (1,6)  (1,7)  (1,8)  (1,9)  (1,10)
 (2,6)  (2,7)  (2,8)  (2,9)  (2,10)
 (3,6)  (3,7)  (3,8)  (3,9)  (3,10)
 (4,6)  (4,7)  (4,8)  (4,9)  (4,10)
 (5,6)  (5,7)  (5,8)  (5,9)  (5,10)

或者,也许我想为每个值生成一组值和一个布尔代码:

Or, maybe I want to generate a set of values and a boolean code for each:

5x2 Array
1 false
2 false
3 false
4 false
5 false

同样,我似乎只能用{(x, y) for x in 1:5, y=false}创建一个元组数组.如果删除x, y周围的括号,则会得到ERROR: syntax: missing separator in array expression.如果将x, y包装在某种东西中,则始终会得到此类输出-ArrayArray{Any}Tuple.

Again, I can only seem to create an array of tuples with {(x, y) for x in 1:5, y=false}. If I remove the parens around x, y I get ERROR: syntax: missing separator in array expression. If I wrap x, y in something, I always get output of that kind -- Array, Array{Any}, or Tuple.

我的猜测:有些东西我只是不来这里.有人愿意帮助我了解什么吗?

My guess: there's something I just don't get here. Anybody willing to help me understand what?

推荐答案

我认为理解力不适合您要尝试做的事情.可以在Julia手册的数组理解部分中找到原因. :

I don't think a comprehension is appropriate for what you're trying to do. The reason can be found in the Array Comprehension section of the Julia Manual:

A = [ F(x,y,...) for x=rx, y=ry, ... ]

这种形式的含义是F(x,y,...)是使用变量x,y等求值的,这些变量采用给定值列表中的每个值.可以将值指定为任何可迭代的对象,但通常将是范围如1:n或2:(n-1),或者是值的显式数组,例如[1.2、3.4、5.7]. 结果是一个Nd密集数组,其维数是变量范围rx,ry等的维数的串联,并且每个F(x,y,...)评估都返回一个标量.

这里的一个警告是,如果您将一个变量设置为> 1维数组,则它似乎首先被展平;因此,结果是...一个数组,其维度是变量范围rx,ry等的维数的并置"的说法并不是很准确,因为如果rx为2x2且ry为3,则您不会得到2x2x3的结果,而是4x3.但是,鉴于以上所述,您得到的结果应该是有道理的:您将返回一个元组,所以这就是Array单元中的内容.返回的元组不会自动扩展到Array的行中.

A caveat here is that if you set one of the variables to a >1 dimensional Array, it seems to get flattened first; so the statement that the "the result is... an array with dimensions that are the concatenation of the dimensions of the variable ranges rx, ry, etc" is not really accurate, since if rx is 2x2 and ry is 3, then you will not get a 2x2x3 result but rather a 4x3. But the result you're getting should make sense in light of the above: you are returning a tuple, so that's what goes in the Array cell. There is no automatic expansion of the returned tuple into the row of an Array.

如果要从综合中获取5x2数组,则需要确保x的长度为5,而y的长度为2.然后每个单元格将包含函数的结果使用xy中的每个可能的元素对作为参数进行求值.事实是,示例数组的单元格中的值实际上不需要评估两个参数的函数.相反,您想要做的只是将两个预定的列粘贴到2D数组中.为此,请使用hcat或文字:

If you want to get a 5x2 Array from a comprhension, you'll need to make sure x has a length of 5 and y has a length of 2. Then each cell would contain the result of the function evaluated with each possible pairing of elements from x and y as arguments. The thing is that the values in the cells of your example Arrays don't really require evaluating a function of two arguments. Rather what you're trying to do is just to stick two predetermined columns together into a 2D array. For that, use hcat or a literal:

  • hcat(1:5, 6:10)
  • [ 1:5 5:10 ]
  • hcat(1:5, falses(5))
  • [ 1:5 falses(5) ]
  • hcat(1:5, 6:10)
  • [ 1:5 5:10 ]
  • hcat(1:5, falses(5))
  • [ 1:5 falses(5) ]

如果要创建一个2D数组,其中第2列包含对第1列求值的函数的结果,则可以这样理解:

If you wanted to create a 2D Array where column 2 contained the result of a function evaluated on column 1, you could do this with a comprehension like so:

f(x) = x + 5
[ y ? f(x) : x for x=1:5, y=(false,true) ]

但这有点令人困惑,我觉得这样做似乎更直观

But this is a little confusing and it seems more intuitive to me to just do

x = 1:5
hcat( x, map(f,x) )

这篇关于Julia中的多维数组理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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