Matlab学习笔记--常用命令

文章目录

  • 前言
  • 一、基本操作与矩阵输入
    • 1.数据类型
    • 2.优先级
    • 3.Format
    • 4.Array Indexing
    • 5.Colon Operator
    • 6.特殊矩阵
    • 7.矩阵相关函数
  • 二、结构化程序和自定义函数
    • 1.Flow Control
    • 2.相关逻辑运算
    • 3.if elseif else
    • 4.switch
    • 5.while
    • 6.for
    • 7.break
    • 8.小技巧
    • 9.自定义函数
    • 10.多输入输出函数
    • 11.Function Defaullt Varialbes
    • 12.Function Handles
  • 三、变量与存取
    • 1.数据类型转换
    • 2.Character(char)
    • 3.String
    • 4.逻辑运算
    • 5.Structure
    • 6.Nesting structures(嵌套结构体)
    • 7.Cell Array
    • 8.Accessing Cell Array 读取Cell数据


前言

Matlab常用命令。


一、基本操作与矩阵输入

1.数据类型

logical
char
numeric:
  int8、uint8、single、double
  int16、uint16、
  int32、uint32、
  int64、uint64
  默认数据类型double
cell
struct

2.优先级

Variable
Built-in function
SubFunction
Private function:
  MEX-file
  P-file
  M-file

3.Format

Style Example
short 3.1416
long 3.141592653589793
shortE 3.1416e+00
longE 3.141592653589793e+00
bank 3.14
hex 400921fb54442d18
rat 355/113

4.Array Indexing

索引从1开始计数。

A = [1 21 6;
		 5 17 9;
		 31 2 7]
A(8) = 9
A([1 3 5]) = 1 31 17
A([1 3;1 3]) = 1 31;1 31
A(3,2) = 2
A([1 3],[1 3]) = 1 6;31 7

5.Colon Operator

1到100

A = 1:100

6.特殊矩阵

eye(n):对角线矩阵
zeros(n1,n2):n1n2零矩阵
ones(n1,n2):n1
n2全1矩阵
diag():diagonal矩阵
rand():随机矩阵

7.矩阵相关函数

max(A)
max(max(A))
min(A)
sum(A)
mean(A)
sort(A)
sortrows(A)
size(A)
length(A)
find(A)

二、结构化程序和自定义函数

1.Flow Control

if,elseif,else Execute statements if condition is true
for Execute statements specified number of times
switch,case,otherwise Execute one of several groups of statements
try,catch Execute statements and catch resulting errors
while Repeat execution of statements while condition is true
break Terminate execution of for or while loop
continue Pass control to next iteration of for or while loop
end Terminate block of code,or indicate last array index
pause Halt executionnn temporarily
return Return control to invoking function

2.相关逻辑运算

Opeerator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not Equal to
&& And
ll Or

3.if elseif else

if condition1
	statement1
elseif condition2
	statement2
else
	statement3
end

4.switch

switch expression
case value1
	statement1
case value2
	statement2
.
.
otherwise
	statement
end	

5.while

while expression
	statement
end

6.for

for variable= start:increment:end
	commands
end

7.break

在while中直接跳出循环到达end

x = 2;k = 0;error = inf;
error_threshold = 1e-32;
while error > error_threshold
	if k > 100
		break
	end
	x = x - sin(x)/cos(x);
	error = abs(x - pi);
	k = k+ 1
end

8.小技巧

clear all 清除所有变量
close all 关闭所有图形窗口
;隐藏命令窗口输出
… 换行
Ctrl + C终止运行脚本

9.自定义函数

自由落体

function x = freebody(x0,v0,t)
x = x0 + v0.*t + 1/2*9.8*t.*t;

10.多输入输出函数

function [a,F] = acc(v2,v1,t2,t1,m)
a = (v2-v1)./(t2-t1);
F = m.*a;

11.Function Defaullt Varialbes

inputname Varialbe name of function inpu
mfilename File name of currently running function
nargin Number of function input arguments
nargout Number of function output arguments
varargin Variable length input argument list
varargout Variable length output argument list

12.Function Handles

f = @(x) exp(-2*x);
x = 0:0.1:2;
plot(x,f(x));

三、变量与存取

1.数据类型转换

函数 功能
double() 转换成double类型
single() 转换成单精度
int8() 转换成8位符号整型
int16() 转换成16位符号整型
int32() 转换成32位符号整型
int64() 转换成64位符号整型
uint8() 转换成8位无符号整型
uint16() 转换成16位无符号整型
uint32() 转换成32位无符号整型
uint64() 转换成64位无符号整型

2.Character(char)

s1 = 'h'
whos
uint16(s1)
s2 = 'H'
whos
uint16(s2)

3.String

s1 = 'Example';
s2 = 'String';
字符串拼接
s3 = [s1 s2];
s4 = [s1;s2];

4.逻辑运算

str = 'aardvark'
'a' == str
1 1 0 0 0 1 0 0

%将a替换成Z
str(str == 'a') = 'Z'

5.Structure

>> student.name = 'John Doe';
>> student.id = '[email protected]';
>> student.number = 301073268;
>> student.grade = [100,74,73;95,91,85.5;100,98,72];
>> student
>
  包含以下字段的 struct:

      name: 'John Doe'
        id: '[email protected]'
    number: 301073268
     grade: [3×3 double]
>> student(2).name = 'Ann Lane';
>> student(2).id = '[email protected]';
>> student(2).number = 301078853;
>> student(2).grade = [95 100 90 ;95 82 97 ;100 85 100];
>student
>  包含以下字段的 1×2 struct 数组:

    name
    id
    number
    grade
函数 功能
cell2struct Convent cell array to structure array
fieldnames Field names of structure,or public fields of object
getfield Field of structure array
isfield Determine whether inupt is structure array field
isstruct Determine whether input is structure array
orderfields Order fields of structure array
rmfield Remove fields from structure
setfield Assign values to structure array field
struct Create structure to cell array
struct2cell Convert structure to cell array
structfun Apply function to each field of scalar structure

6.Nesting structures(嵌套结构体)

>> A = struct('data',[3 4 7;8 0 1],...
              'nest',struct('testnum','Test1','xdata',[4 2 8],'ydata',[7 1 6]));
>> A(2).data = [9 3 2;7 6 5];
>> A(2).nest.testnum = 'Test 2';
>> A(2).nest.xdata = [3 4 2];
>> A(2).nest.ydata = [5 0 9];

7.Cell Array

>> A(1,1) = {[1 4 3;0 5 8;7 2 9]};
>> A(1,2) = {'Anne Smith'};
>> A(2,1) = {3 + 7i};
>> A(2,2) = {-pi:pi:pi};
>> A

A =

  2×2 cell 数组

    {3×3 double        }    {'Anne Smith'}
    {[3.0000 + 7.0000i]}    {1×3 double  }
>> A{1,1} = [1 4 3; 0 5 8;7 2 9];
>> A{1,2} = 'Anne Smith';
>> A{2,1} = 3+7i;
>> A{2,2} = -pi:pi:pi;
>> A

A =

  2×2 cell 数组

    {3×3 double        }    {'Anne Smith'}
    {[3.0000 + 7.0000i]}    {1×3 double  }

8.Accessing Cell Array 读取Cell数据

>> A(1,1)

ans =

  1×1 cell 数组

    {3×3 double}

>> A{1,1}

ans =

     1     4     3
     0     5     8
     7     2     9

函数 功能
cell Create cell array
cell2mat Convert cell array to numeric array
cell2struct Convert cell array to structure array
celldisp Cell array contents
cellfun Apply function to each cell in cell array
cellplot Graphically display structure of cell array
cellstr Create cell array of strings from character array
iscell Determine whether input is cell array
mat2cell Convert array to cell array with different sized cells
num2cell Convert array to cell array with consistently sized cells
struct2cell Convert structure to cell array
>> a = magic(3)
a =
     8     1     6
     3     5     7
     4     9     2
>> b = num2cell(a)
b =
  3×3 cell 数组
    {[8]}    {[1]}    {[6]}
    {[3]}    {[5]}    {[7]}
    {[4]}    {[9]}    {[2]}
>> c = mat2cell(a,[1 1 1],3)
c =
  3×1 cell 数组
    {1×3 double}
    {1×3 double}
    {1×3 double}

你可能感兴趣的:(matlab,学习,矩阵)