当前位置:网站首页>Matlab | those matlab tips you have to know (I)
Matlab | those matlab tips you have to know (I)
2022-07-28 00:28:00 【slandarer】
1: Preset symbolic variable types
Suppose we write the following code :
syms x
f=x^3+x^2+9*x+9;
solve(f==0)
At this time, the solution result is :
ans =
-1
-3i
3i
But if you preset x It's a real number :
syms x
assume(x,'real')
f=x^3+x^2+9*x+9;
solve(f==0)
The result is :
ans =
-1
There are many other presets waiting for you to study :
| Assume ‘x’ is | Syntax |
|---|---|
| real | assume(x,‘real’) |
| rational | assume(x,‘rational’) |
| positive | assume(x,‘positive’) |
| positive integer | assume(x,{‘positive’,‘integer’}) |
| less than -1 or greater than 1 | assume(x<-1) |
| an integer from 2 through 10 | assume(in(x,‘integer’) & x>2 & x<10) |
| not an integer | assume(~in(z,‘integer’)) |
| not equal to 0 | assume(x ~= 0) |
| even | assume(x/2,‘integer’) |
| odd | assume((x-1)/2,‘integer’) |
| from 0 through 2π | assume(x>0 & x<2*pi) |
| a multiple of π | assume(x/pi,‘integer’) |
2:if turn switch ( Logical value as input variable )
Suppose you write the following code :
score=68
if 90<=score&&score<=100
disp(' good ');
elseif 70<=score&&score<90
disp(' good ');
elseif 60<=score&&score<70
disp(' commonly ');
elseif score<60
disp(' fail, ');
end
There is a way to avoid a lot of if else:
Is to use logical value as input switch:
score=68
switch true
case 90<=score&&score<=100
disp(' good ');
case 70<=score&&score<90
disp(' good ');
case 60<=score&&score<70
disp(' commonly ');
case score<60
disp(' fail, ');
end
3:matlab After fitting the curve, it is converted into an anonymous function
x=[2 2.5 3 3.5 4 4.5 5 5.5 6];
y=[41 38 34 32 29 28 25 22 20];
p=polyfit(x,y,3);
f=matlabFunction(poly2sym(p))
xx=2:.1:6;
plot(xx,f(xx),'LineWidth',2)
grid on
f =
With the following values function_handle:
@(x)x.(-8.57e+2./5.4e+1)+x.2.*(2.56e+2./9.9e+1)-x.3.(5.8e+1./2.97e+2)+7.05e+2./1.1e+1

4: View the functions contained in each module faster
If you haven't read Miss Liu's article , I may never click on this little triangle , This triangle is too detailed, Ba hiahiahia picture , After clicking on it, an electronic dictionary will appear help A collection of documents :

5:sub2ind And ind2sub
Suppose we want to put all zero matrix number (2,3) Elements and (3,2) The first element becomes 1, If you write the following code :
A=zeros(4,4);
A([2,3],[3,2])=1
A =
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
We will find that not only two elements have been changed , But we can pass sub2ind The function changes the bivariate index to the univariate index , for example ([4,4] Is the matrix size ):
A=zeros(4,4);
A(sub2ind([4,4],[2,3],[3,2]))=1
A =
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 0
ind2sub Function can be used to reduce the number of nested loops , A simple example , Suppose you write the following code :
for i1=1:5
for i2=1:3
for i3=1:2
disp([i1,i2,i3])
end
end
end
It can be rewritten as the following code ( Not much use and need to pay attention to the order , Far less important than the previous one ):
sz=[5,3,2];
for i=1:prod(sz)
[i3,i2,i1]=ind2sub(sz(end:-1:1),i);
disp([i1,i2,i3])
end
6: Brush the data
No one has not ordered this small brush yet :

Click to highlight some data , Then right click on the highlighted data , You can export the data , Color change , Or delete the data directly , Is there any interactive graphics killer :

7: The size is 0 Matrix
For example, I want to create a [x1,y1;x2,y2;x3,y3;… …] Such a matrix , Add new points at the end of each round , But the number of points added each time is unknown , The number of times to add is unknown , We might as well create a 0xn The size of the matrix continues to add new elements to the back :
% establish 0 That's ok 2 Column matrices
P=zeros(0,2);
% Cyclic random 5-10 Between random wheels
for i=1:randi([5,10],[1,1])
% Increase randomly each time 2-5 Row random number
P=[P;rand(randi([2,5],[1,1]),2)]
end
8: Any base to base conversion
It is to convert to decimal system first , Then convert to other base numbers :
% 12 Turn into the system 16 Base number
bs1=12;
bs2=16;
% Original character
baseStr='1B';
% transformation
decStr=base2dec(baseStr,bs1);
result=dec2base(decStr,bs2)
% result='17'
So there is a simpler RGB Color 、16 Binary code mutual conversion function :
RGB It's worth turning 16 Binary code function :
function HEX=RGB2HEX(RGB)
hexVec=dec2base(RGB,16)';
HEX=['#',hexVec(:)'];
end
call :
hex=RGB2HEX([251,255,250])
% hex = '#FBFFFA'
16 Binary code conversion RGB Value function :
function RGB=HEX2RGB(HEX)
hexVec=reshape(HEX(2:end),2,[])';
RGB=base2dec(hexVec,16)';
end
call :
rgb=HEX2RGB('#FBFFFA')
% rgb = [251,255,250]
9: Virtual plane drawing
MATLAB Support virtual plane drawing , Is the real part correspondence X Axis coordinates , The imaginary part corresponds to Y Axis coordinates , Using virtual plane drawing, you can draw implicit functions with only one array , for instance , Draw a circle with a virtual plane drawing :
t=0:pi/100:2*pi;
plot(exp(t.*1i))

10:MATLAB High precision calculation
commonly MATLAB If the difference between the two numbers is less than eps, It is considered that the value is the same , For example, the following two judgments MATLAB Will think it is right :
1==1+eps/2
1==1+1e-20
ans =
logical
1
ans =
logical
1
eps The value of is 2.2204e-16, As long as we set the accuracy higher, the above problems will not occur , for example :
digits(50)
logical(vpa(1)==vpa(1)+vpa(1e-20))
ans =
logical
0
Another example , I want to show the front of PI 100 Decimal place :
digits(100)
pi100=vpa(pi)
pi100=3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
边栏推荐
- 新媒体内容输出方式-短视频
- The latest ijcai2022 tutorial of "figure neural network: foundation, frontier and application"
- 永州清洁级动物实验室建设选址注意事项
- 智能便利店带你解锁未来科技购物体验
- MATLAB如何将k线图设置为经典红绿配色?
- MATLAB | 那些你不得不知道的MATLAB小技巧(四)
- liux常用命令(查看及其开放防火墙端口号+查看及其杀死进程)
- JS event propagation capture stage bubbling stage onclick addeventlistener
- 永州出入境检验实验室建设那些事
- 火狐浏览器 Firefox 103 发布,提升高刷新率显示器下的性能
猜你喜欢

Cache and MMU management

New media content output method - short video

MATLAB | 那些你不得不知道的MATLAB小技巧(一)

抖音直播监控-循环值守24小时-直播弹幕

MATLAB | 那些你不得不知道的MATLAB小技巧(三)

MQTT----mqtt.fx客户端软件

服务器中毒了——菜是原罪

MFC prompts that this application has requested the runtime to terminate it in an unused way editbox box has been deleted and is still in use
![[geek challenge 2019] rce me](/img/ff/aff58f40f2051f7415d1e16517f824.png)
[geek challenge 2019] rce me

学yolo需要什么基础?怎么学YOLO?
随机推荐
Yongzhou plant cell laboratory construction layout plan
JS ATM机输出
MATLAB 文件夹前面的+和@是干啥的 命名空间与函数的重载
[development tutorial 11] crazy shell arm function mobile phone timer experimental tutorial
MFC prompts that this application has requested the runtime to terminate it in an unused way editbox box has been deleted and is still in use
MATLAB如何将k线图设置为经典红绿配色?
See how well-known enterprises use Web3 to reshape their industries
҈直҈播҈预҈告҈ |҈ 炎热盛夏,与Nono一起跨越高温“烤”验吧!
图片提取文字很神奇?试试三步实现OCR!
看知名企业们如何利用 Web3进行产业重塑
Those "experiences and traps" in the data center
R语言R原生plot函数和lines函数的主要参数说明、解析(type、pch、cex、lty、lwd、col、xlab、ylab)
What are the software operation and maintenance monitoring?
JVM memory model
CSDN21天学习挑战赛
荣耀多款产品齐发,笔记本MagicBook V 14售价6199元起
Glory launched a number of products at the same time. The price of notebook magicbook V 14 starts from 6199 yuan
这种动态规划你见过吗——状态机动态规划之股票问题(中)
How difficult is it to apply for a doctorate under the post system in northern Europe?
英特尔AI实践日第56期 | 探讨行业发展新趋势