MATLAB实验-MATLAB 语言的程序设计

1、学习使用help命令:在命令窗口输入help rand,根据帮助说明学习使用指令rand,然后随机生成一个2×6的数组,观察command window、command history和workspace等窗口的变化结果。(其它不会用的指令,依照此方法类推)

help rand
帮助说明:
rand Uniformly distributed pseudorandom numbers.
R = rand(N) returns an N-by-N matrix containing pseudorandom values drawn
from the standard uniform distribution on the open interval(0,1). rand(M,N)
or rand([M,N]) returns an M-by-N matrix. rand(M,N,P,…) or
rand([M,N,P,…]) returns an M-by-N-by-P-by-… array. rand returns a
scalar. rand(SIZE(A)) returns an array the same size as A.
Note: The size inputs M, N, P, … should be nonnegative integers.
Negative integers are treated as 0
R = rand(…, CLASSNAME) returns an array of uniform values of the
specified class. CLASSNAME can be ‘double’ or ‘single’.
R = rand(…, ‘like’, Y) returns an array of uniform values of the
same class as Y.
The sequence of numbers produced by rand is determined by the settings of
the uniform random number generator that underlies rand, RANDI, and RANDN.
Control that shared random number generator using RNG.
rand(N)返回一个N乘N的矩阵,其中包含从开放区间(0,1)上的标准均匀分布中提取的伪随机值。rand(SIZE(A))返回一个与A大小相同的数组。
rand(2,6)

2、学习使用clc、clear,观察command window、command history和workspace等窗口的变化结果。
(1)学习使用clc、clear
Clc作用: clc清除命令窗口(Command Window)中的所有文本,产生一个清晰的屏幕。运行clc后,不能使用命令窗口中的滚动条查看先前显示的文本。但是,可以使用命令窗口中的向上键t从命令历史中收回语句。在MATLAB代码文件中使用clc来始终在屏幕上相同的起始位置显示输出。
Clear作用:从工作区删除项目,释放系统内存。
(2)命令行窗口(Command Window)

(3)命令历史记录( command history)

(4)workspace

3、初步程序的编写练习,新建M-file,保存(自己设定文件名,例如exerc1、exerc2、 exerc3……),学习使用MATLAB的基本运算符、数组寻访指令、标准数组生成函数和数组操作函数。注意:每一次M-file的修改后,都要存盘。
(1)新建M-file
① 在电脑上打开MATLAB,创建一个工作路径文件夹,设置工作路径:
法一:cd查找默认的工作路径,cd(“路径”)可修改;删除方法:cd进行覆盖
法二:pathpool(设置好路径后保存),删除方法:pathpool打开后选择文件夹删除
法三:在工作框中输入路径后选择文件,删除方法:右击,选择从路径中删除
② 选择“主页->新建文件->脚本”
(2)学习使用MATLAB的基本运算符

[5+2*(7-3)]/2^3
ans =1.6250
[(3+4i)*(5+6i)]^0.1
ans =1.4193 + 0.2588
1/0
ans =Inf
(-1)^-1.5
ans = 0.0000 + 1.0000i
0/0
ans = NaN
(3)数组寻访指令
A=[4 10 1 6 2;8 2 9 4 7;7 5 7 1 5;0 3 4 5 4;23 13 13 0 3]
A = 4 10 1 6 2
8 2 9 4 7
7 5 7 1 5
0 3 4 5 4
23 13 13 0 3
B=A([9 14;10 15])
B = 3 4
13 13
(4)标准数组生成函数
zeros(5)——零矩阵
ans =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
ones(5)——1矩阵
ans =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
eye(5)——单位矩阵
ans =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
randn(5)——随机数矩阵
ans =
-1.3077 -1.3499 -0.2050 0.6715 1.0347
-0.4336 3.0349 -0.1241 -1.2075 0.7269
0.3426 0.7254 1.4897 0.7172 -0.3034
3.5784 -0.0631 1.4090 1.6302 0.2939
2.7694 0.7147 1.4172 0.4889 -0.7873
magic(5)——5维魔鬼矩阵
ans =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
pascal(5)——5维pascal矩阵
ans =
1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70
(5)数组操作函数
A
A = 4 10 1 6 2
8 2 9 4 7
7 5 7 1 5
0 3 4 5 4
23 13 13 0 3
diag(A)——返回矩阵A对角元素成列向量
ans = 4
2
7
5
3
flipud(A)——矩阵上下翻转
ans =23 13 13 0 3
0 3 4 5 4
7 5 7 1 5
8 2 9 4 7
4 10 1 6 2
fliplr(A) ——矩阵左右翻转
ans = 2 6 1 10 4
7 4 9 2 8
5 1 7 5 7
4 5 4 3 0
3 0 13 13 23
rot90(A) ——矩阵逆时针翻转90度
ans = 2 7 5 4 3
6 4 1 5 0
1 9 7 4 13
10 2 5 3 13
4 8 7 0 23
tril(A)——提取矩阵A的下三角矩阵
ans = 4 0 0 0 0
8 2 0 0 0
7 5 7 0 0
0 3 4 5 0
23 13 13 0 3
triu(A) ——提取矩阵A的上三角矩阵
ans = 4 10 1 6 2
0 2 9 4 7
0 0 7 1 5
0 0 0 5 4
0 0 0 0 3

4、输入C=1:2:20,则C(i)表示什么?其中i=1,2,3,…,10。
a) 输入C=1:2:20:>> C=1:2:20;
b) i=1,2,3,…,10:>> i=1:10;
c) 查看C(i)表示什么:>> C(i)

5、输入A=[7 1 5; 2 5 6; 3 1 5],B=[1 1 1; 2 2 2; 3 3 3],在命令窗口中执行下列表达式,掌握其含义:
A(2, 3) A(:,2) A(3,:) A(:,1:2:3) A(:,3).*B(:,2) A(:,3)B(2,:) AB A.*B A^2 A.^2 B/A B./A

A=[7 1 5; 2 5 6; 3 1 5];B=[1 1 1; 2 2 2; 3 3 3]; >> A(:,3)B(2,:)
A(2, 3) >> A
B
A(:,2) >> A.*B
A(3,:) >> A^2
A(:,1:2:3) >> A.^2
A(:,3).*B(:,2) >> B/A
B./A

6、二维数组的创建和寻访,创建一个二维数组(4×8)A,查询数组A第2行、第3列的元素,查询数组A第2行的所有元素,查询数组A第6列的所有元素。
a) 创建一个二维数组(4×8)A :>> A=rand(4,8)
b) 查询数组A第6列的所有元素:>> A(2,3)
c) 查询数组A第2行的所有元素:>> A(2,:)
d) 查询数组A第6列的所有元素:>> A(:,6)

7、先自学reshape和fliplr的用法,再书写指令生成下列矩阵,并取出方框内的数组元素。

Syntax
B = reshape(A,sz)
B = reshape(A,sz1,…,szN)
reshape
Reshape array
B = reshape(A,sz) reshapes A using the size vector, sz, to define size(B). For example, reshape(A,[2,3]) reshapes A into a 2-by-3 matrix. sz must contain at least 2 elements, and prod(sz)must be the same as numel(A).
① B = reshape(A,m,n) 将矩阵A的元素返回到一个m×n的矩阵B。如果A中没有m×n个元素则返回一个错误。
② B = reshape(A,m,n,p,…) or B =reshape(A,[m n p …]) 把A中元素进行重塑成m×n×p×…的矩阵,特别地,指定的维数m×n×p×…的积必须与prod(size(A))相同。
③ B = reshape(A,…,[],…) 指定某维数后,剩余的维数长度值用占位符[]来表示,使得维数的乘积等于prod(size(A))。prod(size(A))的值必须被指定维数的乘积平分。prod(size)的数目必须和prod(size(A))相同。
fliplr
Flip array left to right
Syntax
B = fliplr(A)
B = fliplr(A) returns A with its columns flipped in the left-right direction (that is, about a vertical axis).
If A is a row vector, then fliplr(A) returns a vector of the same length with the order of its elements reversed. If A is a column vector, then fliplr(A) simply returns A. For multidimensional arrays, fliplr operates on the planes formed by the first and second dimensions.
B = fliplr(A)返回A,其列按左右方向翻转(即,大约垂直轴)。
B = fliplr(A)返回A,其列按左右方向翻转(即,大约垂直轴)。
如果A是一个行向量,那么fliplr(A)返回一个长度相同的向量,其元素的顺序颠倒。 如果A是一个列向量,那么fliplr(A)简单地返回A。
fliplr:
B = fliplr(A)
将矩阵A的列进行左右翻转矩阵 (绕垂直轴)
如果A是一个行向量,fliplr(A)将A中元素的顺序进行翻转。
如果A是一个列向量,fliplr(A)还等于A。
a) 生成矩阵

A=[1:25]
b=reshape(A,5,5) %先行后列
bT=b’
e=fliplr(bT)
z=bT
z(2,:)=e(2,:)
b) 取出方框内的数组元素
z(2,2:3)
z(2:4,4)
z(4:5,1:3)

8、两种运算指令形式和实质内涵的比较。设有3个二维数组A(2×4),B(2×4),C(2×2),写出所有由2个数组参与的合法的数组运算和矩阵指令。
A=randn(2,4);B=randn(2,4);C=randn(2,2)
① A+B
② A-B
③ A.B
④ A
B’
⑤ A./B
⑥ B./A
⑦ CA
⑧ K=C^2%进行矩阵的乘幂时,矩阵必须是方阵
⑨ M=C^(-0.1)
⑩ N=A.^2
9、用for循环语句实现求1~100的和。
a=0;
for i=1:100
a=a+i;
disp(a)
end
10、用for循环语句实现编写一个求n阶乘的函数文件。
n=input(‘请输入求去的n阶阶乘的阶数:’);
x=1;
for i=2:n
x=x
i;
end
disp(x)
11、Fibonacci数组的元素满足Fibonacci 规则:
且 ,现要求该数组中第一个大于10000的元素。
a(1)=1;
a(2)=1;
i=1;
while a(i)+a(i+1)<10000
a(i+2)=a(i)+a(i+1);
i=i+1;
end
disp(a(i)+a(i+1));

12、已知一维数组A=[2,4,5,8,10],B=[4,6,9,3,4],用for循环语句实现 。求和函数可用sum()。
【方法一】:用变量累加计算的结果:
A=[2,4,5,8,10];
B=[4,6,9,3,4];
n=size(A,2)
sum=0;
for i=1:n
sum=sum+A(i)*B(n-i+1)
end
fprintf(‘sum is: %d \n’,sum);
[fprintf :fprintf(formatSpec,A1,…,An),将多个数值和字面文本输出到屏幕。]
【方法二】:用求和函数对乘积结果的矩阵进行求和运算:
%%方法二:
A=[2,4,5,8,10];
B=[4,6,9,3,4];
n=size(A,2)
for i=1:n
c(i)=A(i)*B(n-i+1)
end
he=sum(c,2)
fprintf(‘sum is: %d \n’,he);
[sum(A): 如果 A 是向量,则 sum(A) 返回元素之和。如果 A 是矩阵,则 sum(A) 将返回包含每列总和的行向量。返回的是行向量。
sum(A,2):是对行求和,返回的是列向量。]

13、利用for循环求1!+2!+3!+ … +5!的值。
i=1;
c=ones(1,5);
for n=1:5
for m=1:n
c(n)=c(n)*m;
end
end
he=sum(c,2);
fprintf(‘sum is: %d \n’,he);

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值