Matlab - 基础使用

这篇博客介绍了Matlab的基础使用,包括基础数学运算如指数、绝对值、三角函数等;矩阵操作,如访问特定元素、行列选取;数据的导入与保存方法;条件语句if else和switch的使用;循环语句for和while;以及自定义函数的创建。此外,还提及了在Matlab中绘制图像的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基础运算

a = 1/2; //0.5
b = 1-2; //-1
c = 2*pi; //6.2832
d = 2^3;//8

常见数学函数

  • exp: exponentiation (e^)
  • abs: absolute value
  • cos, sin, tan :cosine, sine and tangent
  • acos,asin,atan :inverse of cosine, sine and tangent
  • cosh,sinh,tanh: hyperbolic functions (also inverses)
  • sqrt :square root
  • conj :complex conjugate
  • fix round :toward zero
  • round :round toward nearest integer
  • log :natural logarithm
  • log10 :base 10 logarithm
  • real :real part of complex number
  • imag :imaginary part of complex number

矩阵表示

a=[1 2 3;4 5 6];
//1 2 3
//4 5 6
b=[1:1:3] //b = 1 2 3
c=[0:2:10] //c = 0 2 4 6 8 10
d=linspace(-3,3,100); // 返回100个-3~3之间的数
[x,y] = meshgrid(x,y); 
a = a.*3; //a中每个数值都 *3

A(2,3) 第二行第三列
A(2,:) 第二行
A(:,3) 第三列
A(1:3,3:4) 1~3行,3~4列

数据传入,保存

可以使用 importdata(‘a.txt’)
或者load上传数据
使用 save 将workplace数据保存到mat文件。

>> A=importdata('Temp.txt')
A =
    data: [4x4 double]
    textdata: {'R1' 'R2' 'R3' 'R4'}
    colheaders: {'R1' 'R2' 'R3' 'R4'}
>> A.data
ans =
    88.4000 91.5000 89.2000 77.3000
    83.2000 88.0000 67.8000 91.0000
    77.8000 76.3000 90.3000 92.5000
    92.1000 96.4000 81.2000 84.6000
>> A.textdata
ans =
    'R1' 'R2' 'R3' 'R4'

条件语句 if else,switch

if condition1
    statements executed if
    condition1 is true
elseif condition2
    statements executed if
    condition2 is true
else
    statements executed if
    neither condition1 or
    condition2 is true
end
switch switch_expression
case case_expression
    statements
case case_expression
    statements
:
otherwise
    statements
end

循环语句 for, while

for count=0:2:20
    statement;
end
while (isempty(userString) ||userString=='Y' || userString=='y')
    userString = input('Do you want to repeat the loop? y/n :','s');
    if (isempty(userString))
        userString='Y';
    end;
end

函数

自定义函数

function [c] = add(a,b)
    c = a+b;
end

图像

图像基本函数操作

>> t=[0:0.01:10];
>> p1=cos(t);
>> p2=sin(t);
>> p3=p1.*p2;
>> plot(t,p1,t,p2,t,p3)
>> title('Example Plot')
>> xlabel('t (seconds)')
>> legend('cos', 'sin','cos(t)*sin(t)')

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值