matlab 打印_Julia 替代 MATLAB指南:画图、符号运算、文件读取、不打分号显示

bad74a55163b7d11d12536539c8105ef.png

一、画图 http://docs.juliaplots.org/latest/tutorial/#Plotting-in-Scripts

using Plots
# gr()   # use GR as backend
  1. 对数图
p1 = plot(err, yscale=:log10,  label="err_norm")  # notice :log10 instead of log10

具体每个backend 的支持的yscaleReference Supported Attributes · Plots

2. 保存图片

savefig("myplot.png") # Saves the CURRENT_PLOT as a .png
savefig(p, "myplot.pdf") # Saves the plot from p as a .pdf vector graphic

3. 多图 Tutorial · Plots

plot!(...) # http://docs.juliaplots.org/latest/tutorial/#Changing-the-Plotting-Series
# or
y = rand(10, 4)
plot(x, y, layout = (4, 1))
# or
p1 = plot(x, y) # Make a line plot
p2 = scatter(x, y) # Make a scatter plot
p3 = plot(x, y, xlabel = "This one is labelled", lw = 3, title = "Subtitle")
p4 = histogram(x, y) # Four histograms each with 10 points? Why not!
plot(p1, p2, p3, p4, layout = (2, 2), legend = false)

# and more

4. 坐标轴 Axis Attributes · Plots

符号运算——使用JuliaPy/SymPy.jl

目前可用的一个有点老的文档

Home · SymPy​juliahub.com

当前Julia 中的计算还是需要借助Python 的SymPy (两个库,链接不一样)。Julia库SymPy.jl相当于提供wrapper.

  1. 安装调用

首先在Shell中用pip安装python库sympy

pip install sympy

Julia中使用

# Pkg.add("SymPy")
# # run this in 
using LinearAlgebra  # get norm
using SymPy # we will get symbolic norm function automatically

2. 一些简单的入门可以参考

http://mth229.github.io/symbolic.html#Introduction​mth229.github.io Simplification · SymPy​juliahub.com

3. 打印latexSimplification · SymPy3. 打印latex

解决方案使用PyCall 调用 SymPy库中的latex Printing - SymPy 1.6.2 documentation

latex = sympy.latex # define a function we can use these function **latex** to generate latex code

4. 新建变量的几种方式

@vars x ϵ                     # 1
x, ϵ = symbols("x ϵ")      # 2
x, ϵ = Sym("x ϵ")           # 3

5. 矩阵Matrices · SymPy

A= Sym[1 -1; 3 4; 0 2]
A = [Sym(1) -1; 3 4; 0 2]     ## using an annotation to ensure the type. Alternatively, through promotion, just a single symbolic object will result in the same:

A = [1//11 3]  # sometimes can use built in Rational type in Julia

# The following construction should be avoid
# Sym[1 -1; 3 4; 0 2] |> typeof                          # 3x1
# sympy.Matrix([1 2 3;2 3 4;3 4 5 ]) |> typeof     # 3x2
# sympy.Matrix([1 2 3;2 3 4;3 4 5 ])    # . As shown, it is better to avoid the sympy.Matrix constructor when possible, and when not, only pass in a symbolic array created through Julia's array semantics.

6. 矩阵切片与MATLAB 有巨大不同,似乎与Python 相同

要记住所有的向量都会自动的改为列向量!若需行向量需要在算式里手动转

A=[1 2 3 3;2 3 4 5;3 4 5 7; 4 5 6 11]

# want the 1st row
A[1,:] ## give a column vector !!!! 
##4-element Array{Int64,1}:
## 1
## 2
## 3
## 3

A[:,1] * A[1,:]     # gives error, dimension does not match
# The right way to do it
A[:,1] * A[1,:]'        # transpose manually

7. 化简公式

# you'd better add .|> simplify everywhere
x^2 / x .|> simplify  # remember the dot
# without the dot, the performance degrade a lot.

# Notice that += -= /= does not fit very well with .|> and |>
# for example
A[:,I] /= norm(z) .|> simplify       # does not work well. Use A[:,I] = A[:,I] / norm(z) .|> simplify

8. 类似于MATLAB的fplot

例子

plot(x->x^2 - 2x)        # just like fplot MATLAB
# x -> x^2-2x    <--- return a function. This function serves as a parameter in plot
# see https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions
plot([sin cos])             # plot sin and cos in the same canvas
plot(x->x^2 - 2x, 0, 4)

86c88ca517aa40d7a48cf500077f2f31.png

三、显示运算中某行的运算结果

MATLAB中如果不打分号,会显示运算结果,这非常方便debug的时候查看。但有时忘记打分号会输出满屏的输出结果,程序稍微写长一点,再找到哪里有分号,还是挺烦人。

Julia 中为方便的debug,这样,这一行就会达到和 MATLAB 中不打分号的效果。

  1. 用macro @show
@show a = 1+2
# a |> display  # use |>
# print( ...)

2. 单位阵与Matlab不同-- so simple

I # capital i

似乎有什么很好的特性,比 eye(n) 实现要好,可参考

Why `eye` has been deprecated?​discourse.julialang.org
fe5a5d3e64e746c104cb5ccb97856b9d.png
https://github.com/JuliaLang/julia/issues/23156#issuecomment-339788483​github.com

3. 关于括号

A'(b - c) # just don't do that 
d  = b - c
A'd # work

4. Frobenius Norm

norm(A) # Frobenius norm
opnorm(A, 2) # l2 norm

5. 文件读取 ——替代MATLAB中load()函数

  1. 读取 txt 文件,阅读分隔符文件 · Julia中文文档,可知txt等文件
  2. 读写 .mat (MATLAB)文件 使用JuliaIO/MAT.jl 包。PS:应该属于半官方库吧,原因1:首先Github用户名,2.可以在Julia仓库的issue reading MAT files · Issue #805 · JuliaLang/julia 中找到其中一个 developer timholy 的首肯。
  3. 读写 csv 文件,使用 JuliaData/CSV.jl 。
  4. 如果需要确定自己所在的文件夹路径,不想切换到Shell中,也直接使用pwd()ls() 查看路径。

Example 读取txt文件为例

#pwd()
# current directory /home/user/Desktop/
using DelimitedFiles
# data are in /home/user/Desktop/data_problem/
datadir="data_problem/"

# if we want
# /home/user/Desktop/data_problem/data.txt
# /home/user/Desktop/data_problem/label.txt
A = readdlm(datadir * "data.txt") 
y = readdlm(datadir * "label.txt");
# function *   <---- Concatenate strings and/or characters, producing a String.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值