高考考试网
当前位置: 首页 高考资讯

matlab常用操作总结(MATLAB学习之进阶指令)

时间:2023-06-01 作者: 小编 阅读量: 6 栏目名: 高考资讯

#元胞数组基本概念:元胞数组是MATLAB的一种特殊数据类型,将元胞数组看作一种无所不包的通用矩阵,或者叫做广义矩阵。我们用MATLAB将他们保存在2×2的cell元胞数组中,将其当作一个储存信息的“储物柜”。假设需要提取上述2×2的cell数组中的字符串"LearningYard",我们通过类似提取矩阵元素的操作指令进行:cell{2,1}或cell{2}。然后需要注意的是需要把矩阵现用空集进行表示,否则在for循环中无法识别Test_Matrix。

分享兴趣,传播快乐,

增长见闻,留下美好!

亲爱的您,这里是LearningYard学苑。

之前小编带领大家学习了一些MATLAB的基础操作,忘记了的同学赶快补课。

今天小编给大家带来的是一些进阶学习,一起来看看吧!

#元胞数组

基本概念:元胞数组是MATLAB的一种特殊数据类型,将元胞数组看作一种无所不包的通用矩阵,或者叫做广义矩阵。组成元胞数组的元素是任何一种数据类型的常数或者常量,每一个元素也具有不同的尺寸和内存占用空间,每一个元素的内容也完全不同,所以元胞数组的元素叫做元胞(cell)。

适用情境:假设自己需要在MATLAB中保存4组信息,它们分别是:

【实数】6;

【向量】[1 2 3];

【文本】LearningYard;

【矩阵】magic(4)。

我们用MATLAB将他们保存在2×2的cell元胞数组中,将其当作一个储存信息的“储物柜”。

代码编写:新建脚本,采用大括号{ }进行cell数组的创建:

运行后,命令行窗口如下显示:

除此之外,还可通过指令“celldisp”来显示所有元胞的内容。

假设需要提取上述2×2的cell数组中的字符串"LearningYard",我们通过类似提取矩阵元素的操作指令进行:

cell{2,1}或cell{2}。

注:这里需要用到大括号。

那么怎么将元胞数组转化为其他形式呢?

cell2mat 将元胞数组转变成为普通的矩阵

mat2cell 将数值矩阵转变成为元胞数组

num2cell 将数值数组转变成为元胞数组

#循环覆盖值累积

在学习VIKOR算法过程中,我们需要确定群体效用值和个体遗憾值的时候用到了“end 1”和矩阵分行操作,假如在该步骤将“end 1”删掉,那么结果会发生怎样的改变呢?

删掉“end 1”后的代码变化后的结果见下图,截图还没有截全,但我们能清楚地看到Best_value和Worst_value的数值随着语句的循环一直在发生更替,而且它们的属性变成了1×1的double形式,不再是向量了。

使用“end 1”有效避免循环语句中数值覆盖的情况,我们来看一下“end 1”具体的工作流程:

Best_value =

1

Worst_value =

0

Best_value =

1 1

Worst_value =

0 0

Best_value =

1 1 1

Worst_value =

0 0 0

Best_value =

1 1 1 1

Worst_value =

0 0 0 0

它会把数值不断累积,使其变成一个向量。但需要注意的是Best_value和Worst_value这两个变量必须先将其定义为一个空集“[ ]”,否则在end 1步骤中会报错。

#行向量转化为矩阵

通过(end 1)指令编码出来的结果都是行向量的形式,如果要将其转化为矩阵,我们可以将矩阵理解为多个规格相同的行向量进行累积,用代码表示为:

[row1;row2;row3;row4]

将行向量用分号分开,它们就组成了矩阵。

以Test_Vector=[1,2,3,4,5,6,7,8,9]为例,将其变为如下形式的3×3矩阵。

方法很简单,代码表示如下:

Test_Vector=[1,2,3,4,5,6,7,8,9]

Size_Test_Vector=size(Test_Vector);

Test_Vector_Column=Size_Test_Vector(2);

Test_Matrix=[ ];

for i=[1:3:Test_Vector_Column]

Test_Matrix_Change=Test_Vector(i:i 2);

Test_Matrix=[Test_Matrix;Test_Matrix_Change];

end

display(Test_Matrix)

首先定义行向量的规格尺寸,以方便对其分行。然后需要注意的是需要把矩阵现用空集进行表示,否则在for循环中无法识别Test_Matrix。最后在for循环内用change的变量对行向量的内容进行截取,用[Test_Matrix;Test_Matrix_Change]的方式重复对Test_Matrix进行赋值,使其变成矩阵的形式。最终得到的结果如下:

#英文学习

1. Cell array

Basic concept: Cell array is a special data type of MATLAB. Cell array is regarded as a kind of all-encompassing general matrix, or generalized matrix. The elements that make up a cell array are constants or constants of any data type. Each element also has a different size and memory footprint. The content of each element is also completely different. Therefore, the elements of the cell array are called cell (cell ).

Applicable situation:

Suppose you need to save 4 sets of information in MATLAB. They are:

We use MATLAB to store them in a 2×2 cell array, treating it as a "storage cabinet" for storing information.

Code writing: create a new script, use braces {} to create a cell array:

After running, the command line window is displayed as follows:

In addition, the content of all cells can also be displayed through the command "celldisp".

Suppose we need to extract the string "LearningYard" in the above 2×2 cell array, we use similar operation instructions to extract matrix elements:

cell{2,1} or cell{2}.

Note: Braces are needed here.

So how to convert the cell array into other forms?

cell2mat transforms a cell array into a normal matrix

mat2cell transforms a numeric matrix into a cell array

num2cell turns a numeric array into a cell array

2. Cycle coverage value accumulation

In the process of learning the VIKOR algorithm, we need to use the "end 1" and matrix branch operations when determining the group utility value and the individual regret value. If "end 1" is deleted in this step, what will happen to the result? Change?

Delete the code after "end 1"

The result of the change is shown in the figure below. The screenshot has not been truncated yet, but we can clearly see that the values of Best_value and Worst_value are constantly changing as the sentence loops, and their attributes have become 1×1 doubles. It's not a vector anymore.

Use "end 1" to effectively avoid the value coverage in the loop statement. Let's take a look at the specific workflow of "end 1":

It will accumulate the value continuously, making it a vector. But it should be noted that the two variables Best_value and Worst_value must first be defined as an empty set "[ ]", otherwise an error will be reported in the end 1 step.

3. Convert row vector to matrix

The result encoded by the (end 1) instruction is in the form of a row vector. If you want to convert it into a matrix, you need to use the following techniques.

We can understand the matrix as the accumulation of multiple row vectors of the same specification, expressed in code as:

Separate the row vectors with semicolons and they form a matrix.

Take Test_Vector=[1,2,3,4,5,6,7,8,9] as an example, and change it into a 3×3 matrix in the following form.

The method is very simple, the code is as follows:

First define the size of the row vector to facilitate its branching. Then it should be noted that the current matrix needs to be represented by an empty set, otherwise the Test_Matrix cannot be recognized in the for loop. Finally, in the for loop, the content of the row vector is intercepted with the variable of change, and the Test_Matrix is repeatedly assigned by the method of [Test_Matrix;Test_Matrix_Change] to make it into the form of a matrix. The final result is as follows

参考资料:谷歌翻译、《MATLAB入门教程》

本文由LearningYard学苑原创,如有侵权请联系删除。

    推荐阅读
  • 2022年春节可以出国旅行吗(2022年春节能出国旅行吗)

    接下来我们就一起去研究一下吧!2022年春节可以出国旅行吗根据目前国外疫情来看,很多国家都正在经历第二波疫情,很多国家都已经采取封锁措施,所以2022年春节不适合去国外旅游。目前国家对病毒的零容忍政策,和其他国家完全不同,所以就算好不容易能出去,回来也特别麻烦,根本不适合旅游市场。

  • 100岁还在的老艺术家(是演员中的艺术大师)

    光头、高大、肌肉男,银幕中的徐锦江“坏人”“疯狂”的形象深入人心。除了上面那些角色,徐锦江还出演了很多剧然而他还有鲜为人知的另一面↓↓↓生活中徐锦江是如假包换的艺术家,拥有很高水平,如今一幅画甚至已可以卖出80万元的高价!演戏、画画、雕塑样样精通他还有一层不为人知的身份▼▼▼关山月的关门弟子徐锦江原来是搞艺术的。

  • 猪肝怎么吃最健康(猪肝怎么做吃最健康)

    以下内容希望对你有帮助!猪肝怎么吃最健康猪肝做成洋葱炒猪肝最健康。做法:主料:猪肝200克、紫洋葱一个。配料:泡椒50克、泡仔姜50克。

  • 上下五千年之勾践卧薪尝胆(苦心人天不负勾践卧薪尝胆)

    允常逝世后,儿子勾践即位。越王勾践派遣敢死的勇士向吴军挑战,勇士们排成三行,冲入吴军阵地,大呼着自刎身亡。吴王听到消息后,动用全国精锐部队迎击越军,在夫椒大败越军。勾践听后想杀死妻儿,与越国共亡。吴王不听子胥的谏言,终于赦免了越王,撤军回国。吴军大败,越军还杀死吴国的太子。吴国军民疲惫不堪,精锐士兵都在与齐、晋之战中死亡。吴王派公孙雄脱去上衣露出胳膊跪着向前行,

  • 假面骑士agito剧场版有几部(假面骑士agito剧情介绍)

    假面骑士亚极陀:G4计划是平成假面骑士系列首部的剧场版,也是假面骑士亚极陀的剧场版。是纪念假面骑士诞生30周年和东映创立50周年的作品。该剧场版由贺集利树、要润、友井雄亮、秋山莉奈等主演,于2001年9月22日在日本电影院上映。

  • 关于心情的句子短句(关于心情的句子短句有哪些呢)

    很多人这辈子都没有过什么优越感,因为克服自卑就已经用尽了全力。奔跑在社会中,渐渐的都不会再疲倦,有的都是继续,继续。迷失了自己一次,是为了让自己以后更好认清前方的路。等待太久嘚来的东西,多半已经不是当初自己想要的样子了。幸福的人不一定开心,但开心的人一定幸福。因为有些人身在福中不知福。去一座适合居住的城市,有花有草,有你深爱的人!

  • 想给他生孩子他不愿意(我爱上了我的儿子)

    我爱上了我的儿子话说,英国一个报纸昨天登出了这么一个故事…..“我爱上了我的儿子,现在想要给他生孩子”英国人看完以后也是表示又一次被刷新了三观….好吧,事情还要从30年前说起当时英国一个女室内设计师Kim生下了一个男。

  • 简单又好吃的糕点教程(爱吃点心的看过来)

    独立包装携带也方便,上班饿了来一块,扛饿又解馋。而且它家无论是评价和回购率在同行中都是佼佼者。而且苏式点心普遍都做工精湛,模样精致,是下午茶的餐桌上,一道独特的风景线。作为杭州的知名品牌,知味观的苏式点心可以说是家喻户晓,远近闻名。知味观作为杭州知名的中华老字号,是正宗的百年招牌,不仅点心的口味和食材都让人称赞,价格也是非常的亲民。

  • 2020年东阳市高考志愿填报公益讲座详情

    2020年高考已经落幕,新高考改革形势下,不仅全力比分数,填报志愿更是关键一环。面对全国上千所高校、上万个专业和高考政策的新变动,很多考生和家长感到无从下手。讲座主题:填报高考志愿的基本要领讲座时间:2020年7月16日(周四)晚19时讲座地点:东阳中学直播观看方式1、手机点击链接跳转观看。打开“抖音”的“扫一扫”功能,或输入抖音号:xiaobai910,即可观看直播内容。

  • 美国西部游记自驾游(美国西海岸自驾游游记)

    2014年1月,准备美国签证资料,预约面签。两个人一个是阶段性无业游民,一个是敏感行业从业人员。好在经过美使馆的行政审查之后,最终还是顺利拿到了签证。2014年3月,通过国内的中介网站租租车预定了Dollar公司的车。2014年9月,网上买好了环球影城的门票和停车票。于是,在出关的时候被边检美眉拦下了。飞机比预计时间提前了快两个小时抵达了西雅图。总之建议各位还是尽早买AT&T的卡,避免出现上述情况。