朱莉娅语言:子与切片功能 [英] Julia language: sub vs. slice function

查看:218
本文介绍了朱莉娅语言:子与切片功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释深入浅出的 Julia的V0.4功能之间的区别:

Could someone explain in simple terms the difference between julia's v0.4 function:

并的(可能的 sliced​​im

一些简单的例子将大大appriciated。
非常感谢

Some simple example would be greatly appriciated. Thanks a lot

推荐答案

不同的是,丢弃所有层面切片一个标量(非矢量)而经常保留他们。例如:

The difference is that slice drops all dimensions "sliced" with a scalar (non-vector), while sub often retains them. For example:

julia> A = rand(3,3)
3x3 Array{Float64,2}:
 0.403464   0.229403  0.924686
 0.953741   0.175086  0.49139 
 0.0290678  0.705564  0.567355

julia> a = slice(A, 2, :)   # this will be 1-dimensional
3-element SubArray{Float64,1,Array{Float64,2},(Int64,Colon),2}:
 0.953741
 0.175086
 0.49139 

julia> b = sub(A, 2, :)     # this will be 2-dimensional
1x3 SubArray{Float64,2,Array{Float64,2},(UnitRange{Int64},Colon),2}:
 0.953741  0.175086  0.49139

julia> size(a)
(3,)

julia> size(b)
(1,3)

有一个例外:滴尺寸与标索引,如果的他们是尾随的尺寸,这意味着与索引不迟尺寸的一个向量:

There's one exception: sub drops dimensions indexed with a scalar if they are "trailing" dimensions, meaning there are no later dimensions indexed with a vector:

julia> a = slice(A, :, 2)
3-element SubArray{Float64,1,Array{Float64,2},(Colon,Int64),2}:
 0.229403
 0.175086
 0.705564

julia> b = sub(A, :, 2)
3-element SubArray{Float64,1,Array{Float64,2},(Colon,Int64),2}:
 0.229403
 0.175086
 0.705564

julia> size(a)
(3,)

julia> size(b)
(3,)

如果您有一个范围,那么你得到这样的行为

If you slice with a range, then you get behavior like sub:

julia> a = slice(A, 2:2, :)
1x3 SubArray{Float64,2,Array{Float64,2},(UnitRange{Int64},Colon),1}:
 0.953741  0.175086  0.49139

julia> size(a)
(1,3)

这不是重要索引的长度,它的类型:用一个非标量索引任何尺寸将被保留

It's not the length of the index that matters, it's the type: any dimension indexed with a non-scalar will be retained.

这篇关于朱莉娅语言:子与切片功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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