In [2]:
a = [1; 2; 3]
Out[2]:
In [3]:
println(a)
In [4]:
a = [1, 2, 3]
Out[4]:
In [5]:
a = [1 2 3]
Out[5]:
In [6]:
b = [4 5 6]
Out[6]:
In [7]:
A = [1 2 3; 4 5 6]
Out[7]:
In [8]:
A[1,3]
Out[8]:
In [9]:
A[1]
Out[9]:
In [10]:
A[1,1]
Out[10]:
In [11]:
A[1,2]
Out[11]:
In [12]:
A[2,1]
Out[12]:
In [13]:
transpose(A)
Out[13]:
In [14]:
A'
Out[14]:
In [15]:
a = [1; 2; 3;]
c = [7; 8; 9;]
Out[15]:
In [16]:
a'*c
Out[16]:
In [17]:
using LinearAlgebra
In [18]:
dot(a, c)
Out[18]:
In [19]:
Matrix(1.0I, 2, 2)
# I는 identity matrix => 단위 행렬. I가 없으면 에러남
Out[19]:
In [20]:
Matrix(1.0, 2, 2)
In [21]:
zeros(4,1)
Out[21]:
In [22]:
zeros(2,3)
Out[22]:
In [23]:
ones(1,3)
Out[23]:
In [24]:
ones(2,3)
Out[24]:
In [25]:
B = [1 3 2; 3 2 2; 1 1 1]
Out[25]:
In [26]:
inv(B)
Out[26]:
In [27]:
B * inv(B)
Out[27]:
In [28]:
inv(B)[2,1]
Out[28]:
In [29]:
a = [1; 2; 3]
Out[29]:
In [30]:
b = [1.0; 2; 3]
Out[30]:
In [31]:
d = Array{Float64}(undef, 3)
Out[31]:
In [32]:
d[1] = 1
Out[32]:
In [33]:
d[2] = 2
Out[33]:
In [34]:
d[3] = 3
Out[34]:
In [35]:
d
Out[35]:
In [36]:
pairs = Array{Tuple{Int64, Int64}}(undef, 3)
Out[36]:
In [37]:
pairs
Out[37]:
In [38]:
pairs2 = (1, 3)
Out[38]:
In [39]:
pairs[1]
Out[39]:
In [40]:
pairs[1] = (1,2)
Out[40]:
In [41]:
pairs[2] = (2,3)
pairs[3] = (3,4)
Out[41]:
In [42]:
pairs
Out[42]:
In [43]:
# 이거랑 동일함
pairs = [(1,2); (2,3); (3,4)]
Out[43]:
In [44]:
ijk_array = Array{Tuple{Int64, Int64, Int64}}(undef, 3)
Out[44]:
In [45]:
ijk_array[1] = (1, 4, 2)
Out[45]:
In [46]:
a = [10; 20; 30; 40; 50; 60; 70; 80; 90]
Out[46]:
In [47]:
# range
a[1:3]
Out[47]:
In [48]:
# 1부터 9까지 중 +3
a[1:3:9]
Out[48]:
In [49]:
a[3]
Out[49]:
In [50]:
a[2:3:7]
Out[50]:
In [51]:
a[end-2:end]
Out[51]:
In [52]:
b = [200; 300; 400]
Out[52]:
In [53]:
a[2:4] = b
Out[53]:
In [54]:
a
Out[54]:
In [55]:
c = collect(1:2:9)
Out[55]:
In [56]:
println("Hello World")
In [57]:
print("hello "); print("world"); print(" Again")
In [58]:
println("hello "); println("world"); println(" Again")
In [59]:
a = 123.0
Out[59]:
$변수 로 사용 가능. 혹은 $(연산)
In [60]:
println("The value of a = ", a)
In [61]:
println("a is $a, and a-10 is $(a-10).")
In [62]:
b = [1; 3; 10]
Out[62]:
In [63]:
println("b is $b")
In [64]:
println("the second element of b is $(b[2])")
In [65]:
using Printf
In [66]:
@printf("The %s of a = %f", "value", a)
In [67]:
c = [123.12345; 10.983; 1.092312]
Out[67]:
In [68]:
for i in 1:length(c)
@printf("c[%d] = %7.2f\n", i, c[i])
end
In [69]:
str = @sprintf("The %s of a = %f", "value", a)
Out[69]:
In [70]:
println(str)
In [71]:
for i in 1:5
println("This is number $i")
end
for i in I
#do something here for each i
end
In [72]:
for i in 1:5
if i >= 3
break
end
println("This is number $i")
end
In [73]:
my_keys = ["hi", "bye", "cool"]
my_values = ["football", "pocketmon","swim"]
Out[73]:
In [74]:
d = Dict()
for i in 1:length(my_keys)
d[my_keys[i]] = my_values[i]
end
In [75]:
d
Out[75]:
In [76]:
for (key, value) in d
println("$key is a $value player")
end
In [77]:
d["Diego Maradona"] = "football"
Out[77]:
In [78]:
links = [(1,2), (3,4), (4,2)]
Out[78]:
In [79]:
link_costs = [5, 13, 8]
Out[79]:
In [80]:
link_dict = Dict()
Out[80]:
In [81]:
for i in 1:length(links)
link_dict[links[i]] = link_costs[i]
end
In [82]:
link_dict
Out[82]:
In [83]:
for (link, cost) in link_dict
println("Link $link has cost of $cost")
end
In [84]:
function f(x,y)
return 3x+y
end
Out[84]:
In [85]:
f(1,3)
Out[85]:
In [86]:
3*(f(3,2)+f(5,6))
Out[86]:
In [87]:
function my_func(n, m)
a = zeros(n, 1)
b = ones(m, 1)
return a,b
end
Out[87]:
In [88]:
x, y = my_func(3,2)
Out[88]:
In [89]:
x
Out[89]:
In [90]:
y
Out[90]:
In [91]:
sqrt(9)
Out[91]:
In [92]:
sqrt([9 16])
In [93]:
sqrt.([9 16])
Out[93]:
In [94]:
myfunc(x) = sin(x) + 3*x
Out[94]:
In [95]:
myfunc(3)
Out[95]:
In [96]:
myfunc([5 10])
In [97]:
myfunc.([5 10])
Out[97]:
In [98]:
function f(x)
retun x+2
end
In [99]:
function g(x)
return 3x+3
end
Out[99]:
In [100]:
function f(x)
return x+z
end
function run()
z=10
return f(5)
end
Out[100]:
In [101]:
run()
In [102]:
function f(x)
return x+a
end
function run()
return f(5)
end
a=10
run()
Out[102]:
In [103]:
function f2(x)
a=0
return x+a
end
a=5
Out[103]:
In [104]:
println(f2(1))
In [105]:
println(a)
In [106]:
function f3(x)
_a = 0
return x + _a
end
Out[106]:
In [107]:
a = 5
println(f3(1))
println(a)
In [108]:
function f4(x, a)
return x+a
end
a = 5
println(f4(1, a))
println(a)
In [109]:
a = [1 2 3 4 5]
s = 0
for i in 1:length(A)
s += a[i]
end
In [110]:
function my_sum(a)
s = 0
for i in 1:5
s += a[i]
end
return s
end
a = [1; 2; 3; 4; 5]
Out[110]:
In [111]:
my_sum(a)
Out[111]:
In [112]:
rand()
Out[112]:
In [113]:
rand()
Out[113]:
In [114]:
rand(5)
Out[114]:
In [115]:
rand() * 100
Out[115]:
In [116]:
rand(1:10)
Out[116]:
In [117]:
randn(5)
Out[117]:
In [118]:
function my_randn(n, mu, sigma)
return randn(n) .* sigma .+mu
end
Out[118]:
In [119]:
my_randn(10, 50, 3)
Out[119]:
In [120]:
using Pkg
In [121]:
Pkg.add("StatsFuns")
In [122]:
Pkg.build("StatsFuns")
In [123]:
using StatsFuns
In [124]:
mu = 50; sigma = 3;
In [125]:
normpdf(mu, sigma, 52)
In [127]:
data_file_path = "data.txt"
Out[127]:
In [128]:
data_file = open(data_file_path)
Out[128]:
In [129]:
data = readlines(data_file)
Out[129]:
In [130]:
close(data_file)
In [131]:
for line in data
println(line)
end
In [132]:
output_file_path = "results1.txt"
output_file = open(output_file_path, "w")
print(output_file, "Magic Johnson")
println(output_file, "is a basketball player")
println(output_file, "Michael jordan is also a basketball palyer")
close(output_file)
In [134]:
using DelimitedFiles
In [136]:
csv_file_name = "data.csv"
csv_data = readdlm(csv_file_name, ',', header=true)
Out[136]:
In [138]:
data = csv_data[1]
header = csv_data[2]
Out[138]:
In [139]:
header
Out[139]:
In [140]:
data
Out[140]:
In [141]:
start_node = round.(Int, data[:,1])
Out[141]:
In [145]:
end_node = round.(Int, data[:, 2])
Out[145]:
In [146]:
link_length = data[:, 3]
Out[146]:
In [147]:
using PyPlot
In [148]:
# Preparing a figure object
fig = figure()
# Data
x = range(0, stop=2*pi, length=1000)
y = sin.(3*x)
# Plotting with linewidth and linestyle specified
plot(x, y, color="blue", linewidth=2.0, linestyle="--")
# Labeling the axes
xlabel(L"value of $x$")
ylabel(L"\sin(3x)")
# Title
title("Test plotting")
# Save the figure as PNG and PDF
savefig("plot1.png")
savefig("plot1.pdf")
# Close the figure object
close(fig)
In [149]:
lower_bound = [4.0, 4.2, 4.4, 4.8, 4.9, 4.95, 4.99, 5.00]
upper_bound = [5.4, 5.3, 5.3, 5.2, 5.2, 5.15, 5.10, 5.05]
iter = 1:8
# Creating a new figure object
fig = figure()
# Plotting two datasets
plot(iter, lower_bound, color="red", linewidth=2.0, linestyle="-",
marker="o", label=L"Lower Bound $Z^k_L$")
plot(iter, upper_bound, color="blue", linewidth=2.0, linestyle="-.",
marker="D", label=L"Upper Bound $Z^k_U$")
# Labeling axes
xlabel(L"iteration clock $k$", fontsize="xx-large")
ylabel("objective function value", fontsize="xx-large")
# Putting the legend and determining the location
legend(loc="upper right", fontsize="x-large")
# Add grid lines
grid(color="#DDDDDD", linestyle="-", linewidth=1.0)
tick_params(axis="both", which="major", labelsize="x-large")
# Title
title("Lower and Upper Bounds")
# Save the figure as PNG and PDF
savefig("plot2.png")
savefig("plot2.pdf")
# Closing the figure object
close(fig)
In [150]:
# Data
data = randn(100) # Some Random Data
nbins = 10 # Number of bins
# Creating a new figure object
fig = figure()
# Histogram
plt[:hist](data, nbins)
# Title
title("Histogram")
# Save the figure as PNG and PDF
savefig("plot3.png")
savefig("plot3.pdf")
# Closing the figure object
close(fig)
In [ ]: