matlab、2


%% I. 清空环境变量及命令
    clear all
    clc

%% II. MATLAB编程习惯与风格
    x_coordinate = rand(1,10);
    y_coordinate = rand(1,10);
    figure
    plot(x_coordinate,y_coordinate,'r-*')


%  循环体的调试
    a = 1:100;
    b = [];
    for i = 21:21
        index = 105 - 5*i;
        b = [b a(index)];
    end


%%
% 查看、编辑MATLAB自带的工具箱函数
    edit mean

    edit newff


%% IV. 向量化编程
%%
% 1. 及时清除不用的变量
    a = rand(10000);
    b = rand(10000);
    clear a
    b = rand(10000);


%%
% 2. 按列优先循环
    clear all
    clc
    n = 1000;
    a = rand(n);
    tic
    for i = 1:n
        for j = 1:n
            a(i,j);
        end
    end
    toc

    for i = 1:n
        for j = 1:n
            a(j,i);
        end
    end
    toc



%%
% 3. 循环次数多的变量安排在内层
    clear all
    clc
    tic
    a = 0;
    for i = 1:1000
        for j = 50000
            a = a + 1;
        end
    end
    toc

    tic
    a = 0;
    for i = 1:50000
        for j = 1:1000
            a = a + 1;
        end
    end
    toc



%%
% 4. 给一些函数“瘦身”
    edit mean
    clear all
    clc
    a = rand(1,10000);
    tic
    b = mean(a)
    toc

    tic
    c = sum(a)/length(a)
    toc


%% V. 图像对象和句柄
%%
% 1. 如何设置线条的属性呢?
    x = 0:0.01:2*pi;
    y = sin(x);
    h = plot(x,y);
    grid on
    get(h)
    set(h,'linestyle','-','linewidth',2,'color','k')

% 2. 如何修改网格的间隔呢?  
    set(gca,'xtick',0:0.5:7)
    set(gca,'ytick',-1:0.1:1)


% 3. 如何设置图例的字体及大小呢?
    x = 0:0.01:2*pi;
    y1 = sin(x);
    y2 = cos(x);
    plot(x,y1,'r')
    hold on
    plot(x,y2,'-.b')
    h = legend('sin(x)','cos(x)');
    set(h,'fontsize',16,'color','k','edgecolor','r','textcolor','w')


%%
% 4. 如何拆分图例呢?
    x = 0:0.01:2*pi;
    y1 = sin(x);
    y2 = cos(x);
    h1 = plot(x,y1,'r');
    hold on
    h2 = plot(x,y2,'-.b');
    ax1 = axes('position',get(gca,'position'),'visible','off');
    legend(ax1,h1,'sin(x)','location','northwest')
    ax2 = axes('position',get(gca,'position'),'visible','off');
    legend(ax2,h2,'cos(x)','location','northeast')

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值