mirror of
https://gitlab.com/MisterBiggs/julia-for-matlabbers.git
synced 2025-06-16 07:06:48 +00:00
more stuff
This commit is contained in:
parent
bdec34941b
commit
b3a560b4b7
50
intro.jl
50
intro.jl
@ -63,6 +63,8 @@ md"""
|
|||||||
### Growing Arrays
|
### Growing Arrays
|
||||||
|
|
||||||
In Matlab its very common to grow an array in a for loop using `array(end+1)` _(You really should never do this since it creates a new array every loop which gets very inefficent)_. In Julia you use `push!` for adding a single number to the end of a `Vector` and `append!` to add collections to the end of a `Vector`. You'll notice that `!` at the end of the push and append functions. In Julia the exclamation point at the end of a function name means that the function modifies the function inputs in place compared to returning a new copy of the object.
|
In Matlab its very common to grow an array in a for loop using `array(end+1)` _(You really should never do this since it creates a new array every loop which gets very inefficent)_. In Julia you use `push!` for adding a single number to the end of a `Vector` and `append!` to add collections to the end of a `Vector`. You'll notice that `!` at the end of the push and append functions. In Julia the exclamation point at the end of a function name means that the function modifies the function inputs in place compared to returning a new copy of the object.
|
||||||
|
|
||||||
|
> Array index syntax uses square brackets `[]` in Julia.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# ╔═╡ 0a4dc275-04b3-43e3-8a0d-cb921d769a0a
|
# ╔═╡ 0a4dc275-04b3-43e3-8a0d-cb921d769a0a
|
||||||
@ -133,15 +135,20 @@ g() # Uses normal g function since there is no match for g that has a Int and a
|
|||||||
# ╔═╡ 9440e0bc-7920-4b81-b88a-0b1046d24b4c
|
# ╔═╡ 9440e0bc-7920-4b81-b88a-0b1046d24b4c
|
||||||
g(5,6) # Integer method used since inputs are both integers.
|
g(5,6) # Integer method used since inputs are both integers.
|
||||||
|
|
||||||
|
# ╔═╡ 86c23c4e-ab5b-4404-b248-c980a4098676
|
||||||
|
md"""
|
||||||
|
> Something to note in Julia when an array is passed into a function a copy is not made. This means if array `x` is passed into `f(x)`, any changes made to the array will apply to x outside of the array. If you intend to modify the passed in array be sure to add a bang `!` to the end of function name to keep your code idiomatic.
|
||||||
|
"""
|
||||||
|
|
||||||
# ╔═╡ e239604f-2926-455b-a20c-0d9c283cc60a
|
# ╔═╡ e239604f-2926-455b-a20c-0d9c283cc60a
|
||||||
md"""
|
md"""
|
||||||
### Broadcasting
|
### Broadcasting
|
||||||
|
|
||||||
Matlab generally does broadcasting for you which can be dangerous since sometimes its unclear what a function or operator will do when used on arrays. Julia uses the same `.` syntax to do broadcasting but it has to be used anytime you want broadcasting and is very powerful in Julia. all operators support broadcasting `.+ .* .^ .==` and even functions support broadcasting by placing the `.` at the end of the function name `sqrt.(b)`. This is awesome because you can write your function as you normally would for one number, and then if its needs to run on a vector Julia takes are of making your function into a very fast vectorized function! You can still make your own functions that take in vectors since there may be situations where you can write faster code. This means that almost no function in the Julia standard library accepts vectors unlike Matlab. Obviously functions like `maximum` that find the max value in an array take them, but functions like `sin`, `cos`, or `sqrt` all have to use the `.` syntax to work on vectors.
|
Matlab generally does broadcasting for you which can be dangerous since sometimes its unclear what a function or operator will do when used on arrays. Julia uses the same `.` syntax to do broadcasting but it has to be used anytime you want broadcasting and is very powerful in Julia. all operators support broadcasting `.+ .* .^ .==` and even functions support broadcasting by placing the `.` at the end of the function name `sqrt.(b)`. This is awesome because you can write your function as you normally would for one number, and then if it needs to run on a vector Julia takes care of making your function into a very fast and efficient vectorized function! You can still make your own functions that take in vectors since there may be situations where you can write faster code. This means that almost no function in the Julia standard library accepts vectors unlike Matlab. Obviously functions like `maximum` that find the max value in an array take them, but functions like `sin`, `cos`, or `sqrt` all have to use the `.` syntax to work on vectors.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# ╔═╡ 011bb616-3752-432f-adfd-edaecc083cf0
|
# ╔═╡ 011bb616-3752-432f-adfd-edaecc083cf0
|
||||||
b = 1:5
|
b = 1:5 # Ranges are similar to Matlab, but will discussed later in the text
|
||||||
|
|
||||||
# ╔═╡ 6124f6b4-6315-4d74-9067-4553ce1d54bc
|
# ╔═╡ 6124f6b4-6315-4d74-9067-4553ce1d54bc
|
||||||
sin(b) # gotta broadcast!
|
sin(b) # gotta broadcast!
|
||||||
@ -150,7 +157,7 @@ sin(b) # gotta broadcast!
|
|||||||
sin.(b) # Nice!
|
sin.(b) # Nice!
|
||||||
|
|
||||||
# ╔═╡ 384fe37e-a184-4c3f-8b25-3acfee485cfc
|
# ╔═╡ 384fe37e-a184-4c3f-8b25-3acfee485cfc
|
||||||
b .^ 2
|
b .^ 2 # broadcast operators
|
||||||
|
|
||||||
# ╔═╡ 3738b701-1ac6-414e-b1c2-57a1b4a2f53e
|
# ╔═╡ 3738b701-1ac6-414e-b1c2-57a1b4a2f53e
|
||||||
b .== b # Compares values element by element
|
b .== b # Compares values element by element
|
||||||
@ -158,6 +165,30 @@ b .== b # Compares values element by element
|
|||||||
# ╔═╡ ae1134f5-b5cc-49a8-aee0-99ec118299f9
|
# ╔═╡ ae1134f5-b5cc-49a8-aee0-99ec118299f9
|
||||||
b == b # Compares the entire objects against eachother
|
b == b # Compares the entire objects against eachother
|
||||||
|
|
||||||
|
# ╔═╡ 3ef67004-2f85-4aa1-a1b5-5cbf62b292f7
|
||||||
|
md"""
|
||||||
|
### Strict Evaluation
|
||||||
|
|
||||||
|
Matlab evaluates arrays when they are made which is very common in programming languages, but Julia lazily computes values. This means that when you create a range like `1:.1:10` it doesn't create an array immediatly but waits until the array is needed. This has many memory advantages and many functions in Julia use the same behavior. These lazy objects generally behave as arrays but if they don't the `collect` function will turn them into an array.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 1025e3a8-59c9-4725-bc1a-a0b94e6bd6af
|
||||||
|
1:10
|
||||||
|
|
||||||
|
# ╔═╡ e3c28497-29e9-447e-aba0-650aaaa52d88
|
||||||
|
typeof(1:10)
|
||||||
|
|
||||||
|
# ╔═╡ 4fd4f72d-45e0-4760-bc0b-5ebb78636e2c
|
||||||
|
enumerate(1:.25:2) # returns an enumerate object
|
||||||
|
|
||||||
|
# ╔═╡ cf125fe2-c754-4218-92c9-3f491f3109c1
|
||||||
|
collect(enumerate(1:.25:2)) # Returns an an array of tuples as expected
|
||||||
|
|
||||||
|
# ╔═╡ 4802d903-e87c-4ef0-a184-e4ae7af69a28
|
||||||
|
md"""
|
||||||
|
### Random Small Differences
|
||||||
|
"""
|
||||||
|
|
||||||
# ╔═╡ Cell order:
|
# ╔═╡ Cell order:
|
||||||
# ╟─bb461e00-c0aa-11eb-2c7d-1bd1591779c6
|
# ╟─bb461e00-c0aa-11eb-2c7d-1bd1591779c6
|
||||||
# ╟─11ed58ac-1289-4b64-88a8-17cb7f0b6cd2
|
# ╟─11ed58ac-1289-4b64-88a8-17cb7f0b6cd2
|
||||||
@ -165,14 +196,14 @@ b == b # Compares the entire objects against eachother
|
|||||||
# ╟─d15dbf3e-45a8-454f-83eb-e9ad5eac5d24
|
# ╟─d15dbf3e-45a8-454f-83eb-e9ad5eac5d24
|
||||||
# ╟─a6ea55b5-3d1e-4f3c-8b5b-bb6138b6b316
|
# ╟─a6ea55b5-3d1e-4f3c-8b5b-bb6138b6b316
|
||||||
# ╟─863587ab-5ead-4ad0-839f-09e798bc6c93
|
# ╟─863587ab-5ead-4ad0-839f-09e798bc6c93
|
||||||
# ╠═3cad3f5f-f6a5-405b-aeb0-495ebd8cca45
|
# ╟─3cad3f5f-f6a5-405b-aeb0-495ebd8cca45
|
||||||
# ╠═0a4dc275-04b3-43e3-8a0d-cb921d769a0a
|
# ╠═0a4dc275-04b3-43e3-8a0d-cb921d769a0a
|
||||||
# ╠═193c6a29-4f53-4c96-acc7-b5359043b471
|
# ╠═193c6a29-4f53-4c96-acc7-b5359043b471
|
||||||
# ╠═e9a17e1a-652d-452e-8918-86c5adf699eb
|
# ╠═e9a17e1a-652d-452e-8918-86c5adf699eb
|
||||||
# ╠═6fa90db6-9d2b-47bc-9f41-925b447912e8
|
# ╠═6fa90db6-9d2b-47bc-9f41-925b447912e8
|
||||||
# ╠═7ef8ad0b-4724-43d3-86e2-c854c4fab9c4
|
# ╠═7ef8ad0b-4724-43d3-86e2-c854c4fab9c4
|
||||||
# ╠═58c883b6-3682-424d-a1e9-8ac561bb0dbf
|
# ╠═58c883b6-3682-424d-a1e9-8ac561bb0dbf
|
||||||
# ╠═a74e7cad-8dae-41cd-b77a-65ba082956a3
|
# ╟─a74e7cad-8dae-41cd-b77a-65ba082956a3
|
||||||
# ╠═f4d655a2-8539-43f7-9356-56a3e3f9de91
|
# ╠═f4d655a2-8539-43f7-9356-56a3e3f9de91
|
||||||
# ╠═289727e4-fb29-4ba8-97c5-cce62da725bc
|
# ╠═289727e4-fb29-4ba8-97c5-cce62da725bc
|
||||||
# ╠═ec37197c-f18e-48c2-8266-867e28cfc138
|
# ╠═ec37197c-f18e-48c2-8266-867e28cfc138
|
||||||
@ -181,10 +212,17 @@ b == b # Compares the entire objects against eachother
|
|||||||
# ╠═75eeaff0-4fd3-4745-b042-af8ba12a0506
|
# ╠═75eeaff0-4fd3-4745-b042-af8ba12a0506
|
||||||
# ╠═8688fe79-00ad-497f-a466-dc86af9e03b0
|
# ╠═8688fe79-00ad-497f-a466-dc86af9e03b0
|
||||||
# ╠═9440e0bc-7920-4b81-b88a-0b1046d24b4c
|
# ╠═9440e0bc-7920-4b81-b88a-0b1046d24b4c
|
||||||
# ╠═e239604f-2926-455b-a20c-0d9c283cc60a
|
# ╟─86c23c4e-ab5b-4404-b248-c980a4098676
|
||||||
|
# ╟─e239604f-2926-455b-a20c-0d9c283cc60a
|
||||||
# ╠═011bb616-3752-432f-adfd-edaecc083cf0
|
# ╠═011bb616-3752-432f-adfd-edaecc083cf0
|
||||||
# ╠═6124f6b4-6315-4d74-9067-4553ce1d54bc
|
# ╠═6124f6b4-6315-4d74-9067-4553ce1d54bc
|
||||||
# ╠═95e30983-f305-41aa-a1e2-f38fa0d0c1c3
|
# ╠═95e30983-f305-41aa-a1e2-f38fa0d0c1c3
|
||||||
# ╠═384fe37e-a184-4c3f-8b25-3acfee485cfc
|
# ╠═384fe37e-a184-4c3f-8b25-3acfee485cfc
|
||||||
# ╠═3738b701-1ac6-414e-b1c2-57a1b4a2f53e
|
# ╠═3738b701-1ac6-414e-b1c2-57a1b4a2f53e
|
||||||
# ╠═ae1134f5-b5cc-49a8-aee0-99ec118299f9
|
# ╠═ae1134f5-b5cc-49a8-aee0-99ec118299f9
|
||||||
|
# ╟─3ef67004-2f85-4aa1-a1b5-5cbf62b292f7
|
||||||
|
# ╠═1025e3a8-59c9-4725-bc1a-a0b94e6bd6af
|
||||||
|
# ╠═e3c28497-29e9-447e-aba0-650aaaa52d88
|
||||||
|
# ╠═4fd4f72d-45e0-4760-bc0b-5ebb78636e2c
|
||||||
|
# ╠═cf125fe2-c754-4218-92c9-3f491f3109c1
|
||||||
|
# ╠═4802d903-e87c-4ef0-a184-e4ae7af69a28
|
||||||
|
Loading…
x
Reference in New Issue
Block a user