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

分享兴趣,传播快乐,

增长见闻,留下美好!

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

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

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

#元胞数组

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

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

【实数】6;

【向量】[1 2 3];

【文本】LearningYard;

【矩阵】magic(4)。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#循环覆盖值累积

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

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

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

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

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

使用“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步骤中会报错。

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

#行向量转化为矩阵

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

[row1;row2;row3;row4]

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

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

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

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

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进行赋值,使其变成矩阵的形式。最终得到的结果如下:

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

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

#英文学习

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常用操作总结(MATLAB学习之进阶指令)(13)

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

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

,