小源笔记(二十八):Matlab基础知识学习2


分享兴趣,传播快乐,增长见闻,留下美好。

亲爱的您,

这里是LearningYard学苑!

今天小编给大家带来Matlab基础知识学习2,

欢迎您的用心访问!

本期推文阅读时长大约5分钟,请您耐心阅读。

Share interest, spread happiness, increase knowledge, and leave beautiful.

Dear you,

This is the LearningYard Academy!

Today, the editor you Matlab basic knowledge learning 2,

Welcome your visit!

This tweet usually takes about 5 minutes to read. Please be patient and read.


本期小编和大家一起来学习Matlab数据类型中的元胞数组和结构体,让我们一起开始今天的学习吧!

In this issue, Xiaobian and you will learn about cell arrays and structures in Matlab data types. Let's start today's learning together!

元胞数组

元胞数组是Matlab中特有的一种数据类型,是数组的一种,其内部元素可以是属于不同的数据类型,概念理解上,可以认为它和c语言里面的结构体、C++里面的对象很类似。元胞数组是Matlab中的特色数据类型,它不同于其它数据类型(如字符型,字符数组或者叫字符串,以及一般的算术数据和数组)。它特有的存取数据方法决定了它的特点,它有给人一种查询信息的感觉,可以逐渐追踪一直到所有的变量全部翻译成基本的数据信息。它的class函数输出就是cell(细胞之意)。

Cell array is a unique data type in Matlab. It is a type of array. Its internal elements can belong to different data types. Conceptually, it can be considered that it is very similar to the structure in C language and the object in C++. similar. Cell arrays are a special data type in Matlab, which is different from other data types (such as characters, character arrays or strings, and general arithmetic data and arrays). Its unique access data method determines its characteristics. It gives people a feeling of querying information, and can gradually track until all variables are translated into basic data information. Its class function output is cell (meaning cell).


我们直接看其定义,有可能对于新手来说比较抽象。实际上可以将元胞数组想象成为一个一个多层的化妆盒,每层都存在很多抽屉可供储存,但是最终都会归结到这一整个化妆盒中。具体可以看到以下的程序图文介绍。

Let's look directly at its definition, which may be more abstract for novices. You can actually think of a cell array as a multi-layered vanity box, each with many drawers for storage, but ultimately it all boils down to this entire vanity box. Specifically, you can see the following program graphic introduction


1、首先定义一个元胞数组,采用cell(行,列),执行后如下图所示。

1. First define a cell array, using cell (row, column), as shown in the following figure after execution.


2、A{2}定义为在A中1行2列的“盒子”,并且盒子中的内容为eye(3),其中,eye(3)代表的是3*3的单位矩阵,具体执行结果如图所示。

2. A{2} is defined as a "box" with 1 row and 2 columns in A, and the content in the box is eye(3), where eye(3) represents a 3*3 unit matrix, and the specific execution result as the picture shows.

且A的类型为cell。

And the type of A is cell.

3、首先Magic字面意思是魔方,魔术的意思。在Matlab中用来生成n阶幻方。比如三阶幻方就是1-9九个数字,组成一个3*3的矩阵,使得该矩阵无论横、竖还是斜三个方向上的三个数的和总是相同的。幻方是一个很古老的问题。

3. First of all, Magic literally means Rubik's Cube, which means magic. Used in Matlab to generate magic squares of order n. For example, the third-order magic square is nine numbers from 1 to 9, forming a 3*3 matrix, so that the sum of the three numbers in the three directions of the matrix is always the same regardless of the horizontal, vertical or oblique directions. Magic squares are a very old problem.

然后就将此幻方放入A中的第五个盒子中,执行命令,结果如下图所示:

Then put this magic square into the fifth box in A, execute the command, and the result is as shown below:

4、最后将A{5}赋值给B(将A{5}取出来赋值给B),得到如下执行结果:

4. Finally, assign A{5} to B (take A{5} out and assign it to B), and get the following execution result:


结构体


Matlab中的结构体与C语言当中的结构体十分类似,主要用来存储不同类型的数据。因而结构体相当于一个数据容器,把几个不相同的数据类型的数据封装在一个结构体对象中。

The structure in Matlab is very similar to the structure in C language and is mainly used to store different types of data. Therefore, the structure is equivalent to a data container, which encapsulates data of several different data types in a structure object.


1、首先结构体是采用struct来进行表示,在struct后面输入我们的内容,并将其赋给books

1. First, the structure is represented by struct, enter our content after struct, and assign it to books

2、在books中选择name这个属性,得到如下结果:

2. Select the name attribute in books and get the following results:

3、接下来,对比分析books.name(1)和books.name{1}的执行结果,结果显示,前者选择的是cell,后者选择的是字符串。

3. Next, compare and analyze the execution results of books.name(1) and books.name{1}. The results show that the former selects the cell, and the latter selects the string.


今天的分享就到这里了。

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

欢迎给我们留言,

让我们相约明天,

祝您今天过得开心快乐!

That's it for today's sharing.

If you have a unique idea about today’s article,

Welcome to leave us a message,

Let us meet tomorrow,

I wish you a happy day today!


参考资料:谷歌翻译、哔哩哔哩

本文由LearningYard学苑原创,如有侵权请在后台留言!


文案 |Yuan

排版 |Yuan

审核 |Qian

举报
评论 0