목록Julia (11)
자료 매번 검색하기 귀찮아서 만든 블로그

Julia using MAT a = rand(3,3) 3×3 Matrix{Float64}: 0.435371 0.669335 0.487994 0.352952 0.728679 0.664149 0.586168 0.11393 0.400745 matwrite("julia_data.mat", Dict("data"=>a)) MATLAB >> mydata = matfile('julia_data.mat') Properties: Properties.Source: 'julia_data.mat' Properties.Writable: false Properties.ProtectedLoading: false data: [3x3 double] >> mydata.data ans = 0.4354 0.6693 0.4880 0.3530 ..
JLD 패키지 : https://github.com/JuliaIO/JLD.jl GitHub - JuliaIO/JLD.jl: Saving and loading julia variables while preserving native types Saving and loading julia variables while preserving native types - GitHub - JuliaIO/JLD.jl: Saving and loading julia variables while preserving native types github.com 간단한 예시 using JLD a = [1,2,3] save("my.jld", "MyKey", a) my.jld는 Dictionary 형태로 저장되었으며, "MyKey" 에..
예전 버전에서는 sortrows였고, 현재는 sortslices를 사용한다고 한다 원하는 것은 행렬이 주어져있을 때, 우선순위를 정해 여러개의 열에 대해 정렬을 하는 것이다. 예시로 행렬이 다음과 같이 주어졌을 때, julia> t = [1 0 3 ; 1 1 2; 0 0 5; 0 1 10] 4×3 Matrix{Int64}: 1 0 3 1 1 2 0 0 5 0 1 10 아래와 같이 1번 열 → 2번 열의 순서로 데이터를 정렬하고 싶다. 4×3 Matrix{Int64}: 0 0 5 0 1 10 1 0 3 1 1 2 이를 위해 sortslices 함수를 사용한다. 인수의 by 부분에 정렬하고자 하는 열의 우선순위를 적어준다. julia> sortslices(t, dims=1, by=x->(x[1], x[2])..
jld 파일을 다루던 중... julia> a Dict{String, Any} with 5 entries: "4" => [4, 5] "1" => [1, 2] "5" => [5, 6] "2" => [2, 3] "3" => [3, 4] a라는 dictionary는 다음과 같이 생겼다. 내가 원하는 것은 a의 value값만 추출하여 2x5 행렬로 만드는 것이었다. julia> values(a) ValueIterator for a Dict{String, Any} with 5 entries. Values: [4, 5] [1, 2] [5, 6] [2, 3] [3, 4] 일단 values 를 사용하면 value 값만 추출할 수 있다. julia> collect(values(a)) 5-element Vector{Any..
julia> using ProgressBars julia> for i in ProgressBar(1:100) # put some code end 100.0%┣█████████████████████████████████████████████████████████████████████████████████┫ 100/100 [00:00
julia> n = 10^7; julia> A = Array{Float64, 1}() Float64[] julia> @time for i = 1:n push!(A, i) end 0.921990 seconds (30.00 M allocations: 647.505 MiB, 16.98% gc time) julia> B = Array{Float64, 1}() Float64[] julia> sizehint!(B, n) Float64[] julia> @time for i = 1:n push!(B, i) end 0.833846 seconds (30.00 M allocations: 610.336 MiB, 7.58% gc time) sizehint!는 단어의 뜻 그대로 collection (딕셔너리, 배열 등)이 최소한..
flatten과 collect를 사용한다 기본적으로 열 방향으로 나열되기 때문에 행방향으로 해야 할 경우 transpose를 사용하면 된다. julia> matrix = rand(2,2) 2×2 Matrix{Float64}: 0.479909 0.437321 0.190802 0.058017 julia> Iterators.flatten(matrix) Base.Iterators.Flatten{Matrix{Float64}}([0.47990850699513166 0.43732146517532067; 0.19080216284371299 0.05801702572053602]) julia> collect(Iterators.flatten(matrix)) 4-element Vector{Float64}: 0.47990850..
julia> a>0 && println("a is positive!") a is positive! julia> b b==3 || println("b is not 3") b is not 3 a && b : a가 true일 경우에 b를 계산. a가 false인 경우 return 값은 false a || b : a가 false일 경우에 b를 계산. a가 true인 경우 return 값은 true 참고 문헌 https://riptutorial.com/julia-lang/example/15211/short-circuit-operators-----and--- Julia Language Tutorial => Short-circuit operators: && and || Learn Julia Language - Sho..

내가 원하는 타입의 데이터를, 그림을 그리는 함수 (bar, plot 등등)에 넣었을 때 그림이 나오도록 해야하는 상황에서는 @recipe 매크로를 사용하면 편리하다. 예를 들어서, 특정 분포를 plot의 입력값으로 넣었을 때 그 분포에 대한 pdf 그림을 그리고 싶다고 하자. using Plots, Distributions pyplot(size=(400,250)); function default_range(dist::Distribution, n = 4) μ, σ = mean(dist), std(dist) return range(μ - n*σ, stop=μ + n*σ, length=100) end dist = Normal(10,50) 기본적으로 plot 함수에 분포가 들어오면 다음과 같이 에러를 내보낸다..
Julia에서 함수의 입력값을 받을 때 인수의 type을 지정해주는 경우가 있는데, 이 함수에 들어오는 인수의 type에 따라 함수 구조를 바꾸고 싶다고 할 때, (내가) 기본적으로 만들어 왔던 구조는 if를 활용한 코딩이었다. Ex) function foo(InputType) if typeof(InputType)==Int64 #blah blah end if typeof(InputType)==Float64 #blah blah end end 그런데 함수의 입력값에 ::를 사용하면 조금 더 예쁘고 간결한, 가독성 좋은 코드가 된다. Ex) function foo(::Int64, n) println("First argument is Int64 and n is $n !") end function foo(Inpu..