Here is a few nice things that Matlab user can do in Julia

array of ints

In [1]:
x1 = [1:10]
Out[1]:
10-element Array{Int32,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

array of floats

In [9]:
x1 = [1.:10]
Out[9]:
10-element Array{Float64,1}:
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0

Comprehensions

A = [ F(x,y,...) for x=rx, y=ry, ... ]
In [85]:
[i+j for i=1:5, j=1:5]
Out[85]:
5x5 Array{Int32,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10
In [93]:
x = rand(8)
Out[93]:
8-element Array{Float64,1}:
 0.604484
 0.392061
 0.159006
 0.700496
 0.016461
 0.990571
 0.56842 
 0.249582
In [94]:
[ 0.25*x[i-1] + 0.5*x[i] + 0.25*x[i+1] for i=2:length(x)-1 ]
Out[94]:
6-element Array{Any,1}:
 0.386903
 0.352642
 0.394115
 0.430997
 0.641506
 0.594249

Plotting

In [5]:
Pkg.add("Winston")
In [3]:
using Winston
In [4]:
plot(x1)
Out[4]:
In [6]:
plot(x1, rand(10))
Out[6]:
In [7]:
plot(x1, rand(10),"b-o")
Out[7]:
In [13]:
x2 = rand(10,10)
Out[13]:
10x10 Array{Float64,2}:
 0.470593   0.745078   0.00507957  0.55476    …  0.849684  0.61146   0.177914
 0.0176863  0.854575   0.292973    0.416571      0.89902   0.336281  0.139129
 0.559851   0.610792   0.42808     0.118449      0.267995  0.935479  0.234999
 0.250439   0.0527366  0.838703    0.712658      0.561449  0.378895  0.317819
 0.463184   0.629851   0.417069    0.544282      0.159377  0.844525  0.299531
 0.873786   0.860217   0.0142058   0.236449   …  0.789597  0.668284  0.503   
 0.195273   0.179327   0.450923    0.14229       0.263772  0.132346  0.31263 
 0.89076    0.543465   0.0208126   0.922975      0.177622  0.988187  0.931534
 0.236742   0.564753   0.299484    0.0745965     0.111413  0.209411  0.293134
 0.608397   0.684601   0.846688    0.675908      0.613112  0.332819  0.422293
In [16]:
imagesc(x2)
title("random matrix")
Out[16]:

Nice way to express equations

In [27]:
x = 5; y = 3;
In [28]:
3x + 2y
Out[28]:
21
In [29]:
3x
Out[29]:
15

Unicode for everyone

In [31]:
ρ(T,S) = ρ0*(1-α*(T-T0)+β*(S-S0))
Out[31]:
ρ (generic function with 1 method)
In [36]:
ρ0=1025
α = 2.5E-4
T0 = 20.
β = 8.E-4
S0 = 35.
Out[36]:
35.0
In [37]:
ρ(10,35)
Out[37]:
1027.5625
In [38]:
T = [10:15]
Out[38]:
6-element Array{Int32,1}:
 10
 11
 12
 13
 14
 15
In [41]:
S = [35.0:0.1:35.5]
Out[41]:
6-element Array{Float64,1}:
 35.0
 35.1
 35.2
 35.3
 35.4
 35.5

Function is already vectorized

In [43]:
ρ(T,S)
Out[43]:
6-element Array{Float64,1}:
 1027.56
 1027.39
 1027.21
 1027.04
 1026.87
 1026.69
In [46]:
plot(ρ(T,S))
Out[46]:

Strings

In [50]:
st = "Hello there"
Out[50]:
"Hello there"
In [57]:
string(st[1:5]," ",st[7:9])
Out[57]:
"Hello the"

loops

In [63]:
for letter in st
    print(letter, "\n")
end
H
e
l
l
o
 
t
h
e
r
e
In [64]:
for letter=st
    print(letter, "\n")
end
H
e
l
l
o
 
t
h
e
r
e

Evaluation of expressions inside strings

In [69]:
"1 + 2 = $(1 + 2 + S[1])"
Out[69]:
"1 + 2 = 38.0"
In [74]:
"First salinity is $(S[1]) psu"
Out[74]:
"First salinity is 35.0 psu"
In [75]:
"All salinities are: $S"
Out[75]:
"All salinities are: [35.0,35.1,35.2,35.3,35.4,35.5]"

Operators are functions

In [77]:
+(1,2,3,4)
Out[77]:
10
In [78]:
*(1,2,3,4)
Out[78]:
24

Single expression which evaluates several subexpressions in order

In [81]:
z = (x = 1+T[1]; y = 2*S[1]; x + y)
Out[81]:
81.0

a ? b : c

a ? b : c - The expression a, before the ?, is a condition expression, and the ternary operation evaluates the expression b, before the :, if the condition a is true or the expression c, after the :, if it is false.

In [83]:
1 < 2 ? print("yes") : print("no")
yes
In [86]:
danet(x) = (x>0) ? 1 : -1
Out[86]:
danet (generic function with 1 method)
In [87]:
danet(10)
Out[87]:
1
In [88]:
danet(-10)
Out[88]:
-1

Multiple nested for loops can be combined into a single outer loop, forming the cartesian product of its iterables:

In [84]:
for i = 1:2, j = 3:4
         println((i, j))
end
(1,3)
(1,4)
(2,3)
(2,4)

System cals

In [92]:
run(`ls`|`grep U`)
Untitled0.ipynb
Untitled1.ipynb