安玲学记(39)——Matlab入门学习(2)之元胞数组

我想请分析下,安玲学记(39)——Matlab入门学习(2)之元胞数组
最新回答
思檀郎

2024-10-13 07:41:02

分享兴趣,传播快乐,

增长见闻,留下美好。

亲爱的您,这里是LearingYard学苑!

今天小编为您带来Matlab入门学习(2)之元胞数组。

欢迎您的访问!

Share interest, spread happiness,

increase knowledge, and leave beautiful.

Dear, this is the LearingYard Academy!

Today, the editor brings the "Matlab introductory learning (2) of the array of cells".

Welcome to visit!

一、内容摘要(Summary of Content)

本次推文将从内容摘要、思维导图、入门学习来介绍Matlab入门学习(2)之元胞数组,其中入门学习分为知识介绍和软件实操。

This tweet will introduce the cellular array of Matlab introductory learning (2) from content summary, mind map, and introductory learning, of which introductory learning is divided into knowledge introduction and software practice.

二、思维导图(Mind Maps)

三、入门学习(Getting started)

(一)知识介绍(Knowledge introduction)

元胞数组是Matlab的一种特殊数据类型,将元胞数组看作一种无所不包的通用矩阵,或者叫做广义矩阵。组成元胞数组的元素是任何一种数据类型的常数或者常量,每一个元素也具有不同的尺寸和内存占用空间,每一个元素的内容也完全不同,所以元胞数组的元素叫做元胞(cell)。元胞数组的创建方法有3种:直接赋值法、利用函数cell“( )”、利用“{ }”直接创建元胞数组的所有单元。

Cell arrays are a special data type of Matlab, which treats cell arrays as an all-encompassing universal matrix, or generalized matrix. The elements that make up the cell array are constants or constants of any data type, each element also has a different size and memory footprint, and the content of each element is completely different, so the elements of the cell array are called cells. There are three ways to create a cell array: direct assignment method, using the function cell "( )", and using "{ }" to directly create all cells of the cell array.

(二)软件实操(Software hands-on)

1. 采用直接赋值法进行cell数组的创建

1. Use the direct assignment method to create a cell array

(1)内容索引法

(1) Content indexing method

代码如下图所示:

The code is shown in the following figure:

运行结果如下图所示:

The running result is shown in the following figure:

(2)单元索引法

(2) Unit index method

代码如下图所示:

The code is shown in the following figure:

运行结果如下图所示:

(3)利用函数cell“( )”进行cell数组的创建

2. Use the function cell "( )" to create a cell array

利用函数cell()可以创建一个元胞数组,还可以规定其大小。但这只是预定义一个元胞数组,后面需要利用赋值法对元胞的每个单元进行初始化赋值,方法同直接赋值法。

Using the function cell(), you can create an array of cells and specify its size. However, this is only a predefined cell array, and then the assignment method needs to be used to initialize the assignment of each cell cell, which is the same as the direct assignment method.

代码如下图所示:

The code is shown in the following figure:

运行结果如下图所示:

(4)利用“{ }”进行cell数组的创建

3. Create a cell array using "{ }"

代码如下图所示:

The code is shown in the following figure:

运行结果如下图所示:

(5)cell数组的常用相关操作

4. Common related operations for cell arrays

(1)提取数组具体元素

(1) Extract the specific elements of the array

此处提取了相应数组的第一行第二列的内容,代码如下图所示:

Here the inside of the first row and second column of the corresponding array is extracted, the code is shown in the following figure:

运行结果如下图所示:

The running result is shown in the following figure:

(2)元胞数组的转化

(2) Transformation of cell arrays

我们可以用函数cell2mat()将元胞数组转换为普通矩阵。在元胞数组中每个元素是相同的类型并且有相同的大小,才能转换,转换之后的格式遵循原来的格式。mat2cell()能将数值矩阵转变成为元胞数组。num2cell()能将数值数组转变成为元胞数组。此处将元胞数组转换为普通矩阵。

We can use the function cell2mat() to convert an array of cells to a normal matrix. Each element in the cell array is of the same type and has the same size before it can be converted, and the converted format follows the original format. mat2cell() converts a matrix of values into an array of cells. num2cell() converts an array of values into an array of cells. Here, the cell array is converted to a normal matrix.

代码如下图所示:

The code is shown in the following figure:

运行结果如下图所示:

(3)添加元胞数组

(3) Add a cell array

先前我们定义了C是2行2列的元胞数组,此时我们添加索引元胞数组b的第3行第3列,并赋值为17,可以看到此时元胞数组C扩展为3行3列,其他地方是用默认的空矩阵进行填充。

Previously we defined C as a cell array with 2 rows and 2 columns, at this time we add the 3rd row and 3rd column of the index cell array b, and assign the value to 17, we can see that at this time the cell array C is expanded to 3 rows and 3 columns, and the rest of the place is filled with the default empty matrix.

代码如下图所示:

The code is shown in the following figure:

运行结果如下图所示:

(4)修改元胞数组

(4) Modify the cell array

通过给需要修改的元胞赋值来进行修改,代码如下图所示:

Modify it by assigning values to the cells that need to be modified, the code is shown in the following figure:

运行结果如下图所示:

The running result is shown in the following figure:

(5)连接元胞数组

(5) Concatenate cell arrays

首先创建一个与上一个元胞数组维度相同的数字,再进行连接,其中元胞数组E为元胞数组C、D的横向连接,F为元胞数组C、D的纵向连接。

First, create a number with the same dimension as the previous cell array, and then connect, where the cell array E is the horizontal connection of the cell array C and D, and F is the vertical connection of the cell array C and D.

代码如下图所示:

The code is shown in the following figure:

运行结果如下图所示:

今天的分享就到这里了,

如果您对文章有独特的想法,

欢迎给我们留言。

让我们相约明天,

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message,

and let us meet tomorrow.

I wish you a nice day!

参考资料:谷歌翻译、百度百科

本文由LearningYard学苑整理并发出,如有侵权请在后台留言!

文案| Ann

排版| Ann

审核| 王楠鑫