logo

Equivalents Codes in Julia, MATLAB, Python, and R 📂Programing

Equivalents Codes in Julia, MATLAB, Python, and R

Overview

Organizing code that performs the same function in Julia, Matlab, Python, and R.

Let’s assume the following environment for Python.

import numpy as np

General

JuliaMatlabPythonR
comment
#comment%comment#comment#comment
2d grid
X = kron(x, ones(size(y)))
Y = kron(ones(size(x)), y)
[X,Y] = meshgrid(x,y)np.meshgrid(x,y)

Type

줄리아Julia매트랩Matlab파이썬PythonR
type of elements
eltype(x)x.dtype
changing type of elements
convert(Array{Float64},x)x.astype("float64")
type of x
typeof(x)type(x)
#class of x

Vector

줄리아Julia매트랩Matlab파이썬PythonR
column vector
[1 4 -1 2][1;4;-1;2]np.array([1,4,-1,2]).reshape(-1,1)
Row vector
[1;; 4;; -1;; 2]
or
[1 4 -1 2]'
[1 4 -1 2]
or
[1,4,-1,2]
np.array([1,4,-1,2])
Zero vector
zeros(n)
#column vector
zeros(n,1)
#not zeros(n)
#zeros(n)=zeros(n,n)
np.zeros(n)
#row vector
matrix(0,n)
Vector with only ones
ones(n)
#column vector
ones(n,1)
#not ones(n)
#ones(n)=ones(n,n)
np.ones(n)
#row vector
n equally spaced samples (length-based)
range(0,stop=1,length=10)
or
LinRange(0,1,10)
linspace(0,1,10)
#row vector
np.linspace(0,stop=1,num=10)
#row vector
n equally spaced samples (interval-based)
range(0, stop=1, step=0.1)0 : 0.1 : 1np.arange(0,1.001,0.1)
n logarithmically spaced samples
10 .^range(0,1,10)logspace(0,1,10)
#row vector
np.logspace(0,stop=1,num=10,base=10)
#row vector

Matrix

줄리아Julia매트랩Matlab파이썬PythonR
zero matrix
zeros(m,n)zeros(m,n)np.zeros([m,n])matrix(0,m,n)
flatten
vec(x)x(:)np.ravel(x)
return zeros with the same shape and dtype as a given x
zero(x)np.zeros_like(x)
return ones with the same shape and dtype as a given x
fill!(similar(x), 1)np.ones_like(x)
dimensions
ndims(x)ndims(x)x.ndim
size of matrix
size(x)size(x)x.shape
number of elements
length(x)numel(x)x.size
length of largest dimension
maximum(size(x))length(x)max(x.shape)

Random Sampling

줄리아Julia매트랩Matlab파이썬PythonR
Uniformly distributed random vector
rand(n)
#column vector
rand(n,1)
#not rand(n)
#rand(n)=rand(n,n)
np.random.rand(n)
#row vector
Uniformly distributed random matrix
rand(m,n)rand(m,n)np.random.rand(m,n)
Normally distributed random numbers
randn(m,n)randn(m,n)np.random.randn(m,n)

Fourier Transform

Let’s assume the following environment for Julia.

using FFTW
줄리아Julia매트랩Matlab파이썬PythonR
Fourier transform
fft(x)fft(x)np.fft.fft(x)fft(x)
Inverse Fourier transform
ifft(x)ifft(x)np.fft.ifft(x)fft(y,inverse=TRUE)/length(y)

Interpolation

Let’s assume the following environment for Python.

from scipy.interpolate import griddata

Let’s consider $X, Y, P, Z$ as a 2D array and $x, y$ as a 1D array.

줄리아Julia매트랩Matlab파이썬PythonR
2D interpolation
Z=interp2(X,Y,P,x,y)Z=griddata((np.ravel(X),np.ravel(Y)),np.ravel(P),(x,y))

Visualization

Let’s assume the following environment for Julia.

using Plots

Let’s assume the following environment for Python.

import matplotlib.pyplot as plt

Let’s consider $A$ as the array to output as a 2D image.

줄리아Julia매트랩Matlab파이썬PythonR
Set scale range
heatmap(A,clim=(a,b))plt.imshow(A)
plt.colorbar()
plt.clim(a,b)
Horizon line
hline!(a)plt.axhline(a)
Vertical line
vline!(a)plt.axvline(a)