Julia 语言:sub vs. slice 函数 [英] Julia language: sub vs. slice function

查看:11
本文介绍了Julia 语言:sub vs. slice 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能简单的解释一下 julia v0.4 功能的区别:

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

subslice(也可能是 slicedim)

一些简单的例子会很受欢迎.非常感谢

Some simple example would be greatly appriciated. Thanks a lot

推荐答案

不同之处在于 slice 用标量(非向量)丢弃所有切片"的维度,而 sub 经常保留它们.例如:

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)

有一个例外:sub 丢弃用标量索引的维度如果它们是尾随"维度,这意味着没有用向量索引的后续维度:

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,)

如果你 slice 有一个范围,那么你会得到类似 sub 的行为:

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.

这篇关于Julia 语言:sub vs. slice 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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