Julia

2017-04-11 4 views
1

에서 문자열 벡터 내의 요소를 서브 세트하는 벡터를 사용하여 IntegerVector{Integer} 서브 세트 값의 조합을 사용하여 줄리아에서 Vector{String}의 서브 세트를 작성하려고합니다. 기본적으로 "asdf"[1:3]의 하위 집합을 허용하는 함수를 작성하려면 x[y:z]의 세 인수가 각각 벡터 또는 싱글 톤이되어야합니다.Julia

MethodError: no method matching substring(::Array{String,1}, ::Int64, ::Array{Int64,1}) 
Closest candidates are: 
    substring(::Array{String,N}, ::Integer, !Matched::Integer) at untitled-e3b9271a972031e628a35deeeb23c4a8:2 
    substring(::Array{String,1}, ::Integer, !Matched::Array{Integer,1}) at untitled-e3b9271a972031e628a35deeeb23c4a8:13 
    substring(::Array{String,N}, ::Integer, !Matched::Array{Integer,N}) at untitled-e3b9271a972031e628a35deeeb23c4a8:13 
    ... 
in include_string(::String, ::String, ::Int64) at eval.jl:28 
in include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N}) at eval.jl:32 
in (::Atom.##53#56{String,Int64,String})() at eval.jl:50 
in withpath(::Atom.##53#56{String,Int64,String}, ::Void) at utils.jl:30 
in withpath(::Function, ::String) at eval.jl:38 
in macro expansion at eval.jl:49 [inlined] 
in (::Atom.##52#55{Dict{String,Any}})() at task.jl:60 

내가 다른이 있음을 참조하십시오

v = string.('a':'z') 
x = rand(v, 100) .* rand(v, 100) .* rand(v, 100) 

substring(x, 1, 2) 
# or 
substring(x, 1, s) 

내가 오류를 얻을 :

function substring(x::Array{String}, y::Integer, z::Integer) 
    y = fill(y, length(x)) 
    z = fill(z, length(x)) 
    substring(x, y, z) 
end 

function substring(x::Vector{String}, y::Vector{Integer}, z::Integer) 
    y = fill(y, length(x)) 
    substring(x, y, z) 
end 

function substring(x::Vector{String}, y::Integer, z::Vector{Integer}) 
    z = fill(z, length(x)) 
    substring(x, y, z) 
end 

function substring(x::Vector{String}, y::Vector{Integer}, z::Vector{Integer}) 
    for i = 1:length(x) 
    x[i] = x[i][y[i]:min(z[i], length(x[i]))] 
    # If z[i] is greater than the length of x[i] 
    # return the end of the string 
    end 
    x 
end 

그것을 사용하려고 :

내가 지금까지 시도한 것입니다 post addressing 유형이 Vector{String} 인 유사한 오류입니다. 내 게시물도 Vector{Integer}과 관련된 오류에 대한 답변을 묻습니다. 나는 그것에 대한 응답이 추상적 인 유형의 구현이 참되고 어렵다는 것을 아는 나 같은 다른 사람들에게 도움이 될 것이라고 믿습니다.

+2

[Vector {AbstractString} 함수 매개 변수의 가능한 복제본이 julia에서 Vector {String} 입력을 허용하지 않습니다.] (http://stackoverflow.com/questions/21465838/vectorabstractstring-function-parameter-wont-accept-vectorstring- input-in-j) –

+2

이것은 파라 메트릭 불변성의 한 예입니다. 유사한 문제에 대해서는 http://stackoverflow.com/questions/21465838/vectorabstractstring-function-parameter-wont-accept-vectorstring-input-in-j를 참조하십시오. 여기서 문제는'Vector {Integer} '에 있습니다. –

+0

제 질문은 유형 관리 문제로 해석되는 반면에 주목하고 싶습니다. 나는 정말로 제목이 말하는 것을하는 함수를 찾고있다. 더 익숙한 R에서 대답은 'substr (x, 1,2)'입니다.위의 코드를 포함하여 문제를 직접 해결하는 데 합당한 노력을 기울 였음을 보여주었습니다. 문제가 너무 많지 않으면 답을 정말 고맙게 생각합니다. – fsmart

답변

3

당신은 줄리아 0.6에 있다면, 이것은 사용 할 매우 간단합니다 SubString.(strs, starts, ends) :

julia> SubString.("asdf", 2, 3) 
"sd" 

julia> SubString.(["asdf", "cdef"], 2, 3) 
2-element Array{SubString{String},1}: 
"sd" 
"de" 

julia> SubString.("asdf", 2, [3, 4]) 
2-element Array{SubString{String},1}: 
"sd" 
"sdf" 

줄리아 0.5에서 같은 일을 할 수 있지만,이 벡터의 문자열을 포장해야한다 (즉, 그것은 하나의 스칼라로 둘 수 없습니다) :

julia> SubString.(["asdf"], [1, 2, 3], [2, 3, 4]) 
3-element Array{SubString{String},1}: 
"as" 
"sd" 
"df" 

Julia와 R의 주된 차이점은 R에서 함수는 일반적으로 기본적으로 (브로드 캐스트) 벡터로 작업하도록 만들어졌으며, 줄리아에서는 소위 말하는 "도트 콜"을 사용하여 방송 동작을 명시 적으로 지정합니다. f.(x, y, z).

1

생각하기에 매우 일반적인 것으로 생각하면됩니다.

비록 Int64 <: Integer

Array{Int64,1} <: Array{Integer,1}가 아닌

사실이다!


docs on parametric-composite-types은 이유를 자세히 설명합니다. 그러나 이전의 Array{Int64,1}이 메모리 (즉, 많은 인접한 64 비트 값)에서 특정 표현을 가지기 때문에 Array{Integer,1}은 64 비트가 될 수도 그렇지 않을 수도있는 별도로 할당 된 값에 대한 포인터 세트 여야하기 때문에 기본적으로 바꾸는 것입니다.

당신이에/관련 승 줄리아 0.6에서 함수를 선언에 사용할 수있는 새로운 멋진 구문은 유사한 Q & A를 참조하십시오 : Vector{AbstractString} function parameter won't accept Vector{String} input in julia

+0

작동하도록 코드를 변경하는 방법을 알려주시겠습니까? – fsmart