Matlab进阶学习(二)

Matlab进阶学习(二)

本人认为,学习编程语言最有用的就是看非常厉害的人编写的程序,可以从中学习到很多知识与技巧,并且用到自己的程序编写当中。

Matlab函数使用

1、permute 旋转变换三维矩阵

A(:,:,1)=repmat(1,3,3);
A(:,:,2)=repmat(2,3,3);
A(:,:,3)=repat(3,3,3);
A(:,:,1)=

     1     1     1
     1     1     1
     1     1     1

A(:,:,2) =

     2     2     2
     2     2     2
     2     2     2

A(:,:,3) =

     3     3     3
     3     3     3
     3     3     3

At = permute(A,[3,2,1]); %将A的x,y,z变为z,y,x

At(:,:,1) =

     1     1     1
     2     2     2
     3     3     3


At(:,:,2) =

     1     1     1
     2     2     2
     3     3     3

At(:,:,3) =

     1     1     1
     2     2     2
     3     3     3

2、containers.Map 创建对象以及对象属性

num=containers.Map({1,2,3},{'one','two','three'}); %1,2,3为属性名,one,two,three为键值;
num(1)   %调用

ans=

one

3、try catch end 防止程序出现错误而终止运行

try             %先运行try,若没有办法运行,则运行catch,若没办法运行,就继续往下运行
operation....
catch           
operation....
end

4、matcell 将矩阵分块,变成元胞数组

a=rand(60,60);
b=mat2cell(a,[10,20,30],[30,30]);

有意思的代码展示

1、将图片变为视频(可以拿去表白,我上传的资源中有专门的matlab代码表白资源)

%% change the frame to videos to save. 
path = 'C:\Users\Administrator\Desktop\video\picture\';
files = dir([path, '*.jpg']);
count = 0; 

for i=1:size(files, 1) 
    xxx =  strtok(files(i).name, 'M'); 
    image = imread([path, files(i).name]);
    index = sprintf('%04d', i);
    newName = [ index,'.jpg'];

    imwrite(image, [path, newName]);
end

disp('==>> deal with image done !')


jpgFiles = dir([path, '*.jpg']);
videoName = 'C:\Users\Administrator\Desktop\video\Biker_MDNet_OTB100.avi';
fps = 25; %帧率
startFrame = 1; %从哪一帧开始
endFrame = size(jpgFiles, 1); %哪一帧结束

%生成视频的参数设定
aviobj=VideoWriter(videoName);  %创建一个avi视频文件对象,开始时其为空
aviobj.FrameRate=fps;

open(aviobj);%Open file for writing video data

for i=startFrame:endFrame
    frames = imread([path, jpgFiles(i).name]); 
    frames = im2frame(frames); 
    writeVideo(aviobj, frames); 
end
close(aviobj); 

disp('==>> saved the video !')

猜你喜欢

转载自blog.csdn.net/qq_41989587/article/details/82023174