Matlab程序设计总结

1.概述--控制输入和显示方式

变量结果的查询和显示控制 

去掉分号

a

disp(a)

一行输入多条指令

指令的续行输入

续行符...

键盘方向键---命令窗口快捷键

 注释符的使用

 注释 Ctrl+R

取消注释 Ctrl+T

 命令窗口显示格式的控制

精度小数显示  vpa(pi,10)

分数显示 formatn rat


format    默认格式
format short   5字长定点数
format long   15字长定点数
format short e   5字长浮点数
format long e    15字长浮点数
format hex      16进制
format bank      定点货币形式
format rat    小数分数表示
format +       +,-,空格
format compact    压缩空格
format loose       包括空格和空行
format long   15字长定点数
format short e   5字长浮点数
format long e    15字长浮点数
format short g   5位定点或浮点格式
format long g    对双精度,显示15位定点或浮点格式,对单精度,显示7位定点或浮点格式。

清除命令窗口的内容 clc

清除工作空间的所有变量,函数,和MEX文件 clear

关闭所有的Figure窗口 close all

Ctrl + c 终止正在进行的程序

帮助操作 

help plot

doc

demo

 



2.Matlab的数值计算功能

2.1 Matlab 中的数值

2.1.1 数值表示、存储及显示格式     

double \  short (小数默认) \  format (分数近似)

 2.1.2变量查询、清除、保存与获取

save dataab a b

load dataab

load dataab a b

        2.1.3

数值表示:各种形式

数据存储格式:只有一种 double  运算和存储都是双精度格式

数据显示:默认小数点后4位,对于大于1000的实数,用5位有效数字的科学计数形式显示

2.2 变量及其赋值

变量名的命名规则

            

系统变量 

            

                       

2.3 矩阵的表示

     2.3.1 矩阵的输入

  [    ] , ;

复数 x=complex(a,b)

     2.3.2向量的构造

向量的冒泡生成法 a=1:1:10

特殊矩阵的创建函数  ones \  seros \ eye \ rand\ randn\ rand i\ randperm

传送门

 

2.4矩阵的操作

矩阵的拼接

列上补矩阵“逗号”或“空格”

行上补矩阵“分号”

a=[1 2 ;3 4];

b1=repmat(a,1,2)

b1=

    1 2 1 2

    3 4 3 4

b2=repmat(a,2,2)

b2=

     1 2 1 2

     3 4 3 4

     1 2 1 2

     3 4 3 4

矩阵的大小 

size()

length() 

size(a,1)

size(a,2)

numel(A) %A中的元素个数

矩阵元素的表示

双下标 A(i,j)  %第i行地j列

单下标(索引值) A(p)  列向上按序列

        

 

交织和反交织

矩阵的索引

子矩阵的提取

矩阵的第r行   A(r,:)

矩阵的第r列  A(:,r)

矩阵的 i1~i2行 j1~j2列       A(i1:i2,j1:j2)

矩阵的删除

 A(1,:)=[]   %删除第一行

矩阵的转置 

a=1:9
a=reshape(a,3,3)

a =

     1     4     7
     2     5     8
     3     6     9


a' 

a' =

     1     2     3
     4     5     6
     7     8     9


a.'


a.'=

     1     2     3
     4     5     6
     7     8     9

 


b=[1-2*j,2,-j;0,7*j,12;-j*9+10,8,4]

b =

   1- 2i      2        -i
   0           7i       12
  10- 9i     8         4

b'

ans =

   1+ 2i          0        10+0i
   2             - 7i           8 
   i                12           4


b.'

ans =

   1- 2i    0    10- 9i
   2         7i       8
   -i         12      4

< 区别复数矩阵的和   .'   >

矩阵的特殊操作 

矩阵的变维度 reshape(A,m,n)

矩阵的转置   A'

转化矩阵为一列 (按照单下标排一列)   A(:)  

变向fliplr 

 

2.5矩阵的运算

     2.5.1矩阵的数学运算

                

     2.5.2矩阵的点(数组)运算

     2.5.3矩阵的关系和逻辑

    2.6矩阵函数

  

  

   

 



 

3.Matlab基本绘图

      3.1.1 基本二维绘图函数

      3.1.2 带有其他选项的绘图函数

横坐标连续 plot

plot(t,ft)

plot(t,ft,'ro:')

xlabel('t','frontsize',13)

\pi    希腊字母

axis([tmin,tmax,ftmin,ftmax])

hold on 

subplot

figure

横坐标离散 stem

stem(n,fn)

      

 

        3.1.3 二维绘图辅助函数

     

       

        3.1.4 多图像处理

        3.1.5 特殊二维图形处理

单窗口图形绘制   hold on  \\\\   subplot

    

    

 



4. Matlab语言基础

所有数据都用double来存储和运算

tp=imread('lena,bmp')     返回值为包含图像数据的矩阵,且为unit8类型

可对unit等数据类型数组进行基本操作,但是不能执行任何的数学运算,所以必须转化成double类型的

           

字符串变量是指字符向量或矩阵

        

 s1=char('beijing','tianjing','shanghai')

 s2=str2mat('beijing','tianjing','shanghai')

char 和 str2mat 自动补空格,且各串中间必须使用‘,’号,不能用分号

   

求ASCII值

      a=abs('011')

      a =

           48    49    49

dec2bin(data,codelen) 

dec2bin(7,4)
ans =

    '0111'       %字符串

字符串变数值

        A=dec2bin(7,4)-abs('0')

        A =
                 0     1     1     1

bin2dec(data)      % data为字符串

       A=bin2dec('1111')
       A =

                15

 单元和结构阵列

cell

structure

输入

          

输出



5.上机练习

 

 

clc,clear,close all
A=[3 5 7 9; 0 2 4 1;6 4 4 8]
B=[A;5*ones(1,4)]
b=A(1:2,2:3)
A(1:2,:)=[]
C=[A;B(:,1:2)']
D=reshape(C,2,6)




A =

     3     5     7     9
     0     2     4     1
     6     4     4     8

B =

     3     5     7     9
     0     2     4     1
     6     4     4     8
     5     5     5     5

b =

     5     7
     2     4

A =

     6     4     4     8

C =

     6     4     4     8
     3     0     6     5
     5     2     4     5

D =

     6     5     0     4     4     5
     3     4     2     6     8     5

 

clc,clear,close all
x=-2:0.2:2;
y=x.^2;
plot(x,y)
str1=num2str(min(x));
str2=num2str(max(x));
out=['Value of from ',str1,' to ',str2];
xlabel(out)

clc,clear,close all
for i=100:900
    a=num2str(i);
    b=a-'0';
    c=sum(b.^3);
    if c==i
        disp(i);
    end
    
end

 

clc,clear,close all
num2p(12345)
function y=num2p(x)
x=num2str(x);
y=x-'0';
end

clc,clear,close all
a=[1 2 3]
b=[2 3 4]
mult(a,b)
a=[1 2 3 ;2 3 4]
b=[1 2;2 3; 3 4]
mult(a,b)
function y=mult(a,b)
if size(a,1)==size(b,2) &size(a,2)==size(b,1)
    for i=1:size(a,1)
        for j=1:size(b,2)
           summ=0;
           for k=1:size(a,2)
               summ=summ+a(i,k)*b(k,j);
           end
           y(i,j)=summ;
        end
    end
    
elseif size(a)==size(b)
      for i=1:size(a,1)
          for j=1:size(a,2)
              y(i,j)=a(i,j)*b(i,j);
          end
      end
else 
   disp('wrong');
end
end





a =

     1     2     3

b =

     2     3     4

ans =

     2     6    12

a =

     1     2     3
     2     3     4

b =

     1     2
     2     3
     3     4

ans =

    14    20
    20    29

clc,clear,close all
n=input('please input n=\n');
xn=randi([0,1],1,n)
Gt=1/4;
xn=[xn(end-round(Gt*length(xn))+1:end),xn]

clc,clear,close all
t=0:0.5:2.5;
y=t.^2.*[t>=0&t<1]+(t.^2-1).*[t>=1&t<2]+(t.^2-2*t+1).*[t>=3&t<3];
plot(t,y)

clc,clear,close all
n=input("please input n=\n");
datain=randi([0,1],1,n)
data2str(datain)
function strout=data2str(datain)
strout=[];
 for i=1:length(datain)
     if datain(i)==0
          strout=[strout,'abc'];
     else 
          strout=[strout,'ABC'];
     end  
 end
end
clc,clear,close all
A=[0];
while size(A,1)<64
    B=[A,A;A,~A];
    A=B;
end
% size(A)

clc,clear,close all
N=input('please input N=\n');
T=2;
w=2*pi/T;
t=0:0.001:2*T;
ft=0.5;
for i=1:2:N
    ft=ft+1/(pi^2).*cos(i.*w.*t)/i/i;
end
plot(t,ft);
title(['f(t)=',num2str(N)]);

clc,clear,close all
t=-1:0.001:1;
ft=(t+2).*[t>=-1&t<=0]+(2-t).*[t>=0&t<=1]
w=-6*pi:0.01:6*pi;
F=[];
for i=1:length(w)
    F(i)=sum(exp(-j.*w(i).*t).*ft);
end
plot(w,F)

clc,clear,close all
xn=[0,2,6,7,8];
yn=[xn(:,1),xn(:,end:-1:2)]



yn =

     0     8     7     6     2

clc,clear,close all
x=1:10;
y=1:10;
m=length(x);
sumx=sum(x);
sumxx=sum(x.*x);
sumy=sum(y);
sumxy=sum(x.*y);
A=[m,sumx;sumx,sumxx];
Y=[sumy;sumxy];
X=inv(A)*Y;
a=X(1)
b=X(2)

% 
% y=a+b*x
% a =
% 
%   -2.2204e-16
% 
% 
% b =
% 
%     1.0000


 ###这道题有点错,不过思路没问题

clc,clear,close all
s=input('s=\n','s');
score=[25,18,12,10,8,6,4,2,1];
sum=0;
for i=1:length(s)
    if s(i)=='0'
        sum=sum+score(10);
    elseif s(i)~='-'
        sum=sum+score(s(i)-'0')
    end      
end
sum

 

 

clc,clear,close all
fin=fopen('Jack.txt');
n=1;
while ~feof(fin)
   csxx{n}=fgetl(fin);
   n=n+1;
end
fclose(fin);
cj=[];
for k=1:n-1
   kh=csxx{k};
   id=find(kh>='0'&kh<='9');
   cj=[cj,str2num(kh(id(1):end))];
end
[~,cjid]=sort(cj);
cjpx=[];
for p=cjid
   cjpx=char(cjpx,csxx{p});
end
disp(cjpx)

            

    

           

   

                     

  

 

  

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值