您的位置 首页 > 数码极客

matlab如何给元胞赋值、matlab元胞数组赋值


Matab入门学习——

Matlab进阶指令

Getting started with Matab——


Matlab advanced commands



分享兴趣,传播快乐,

增长见闻,留下美好!

亲爱的您,

这里是LearningYard学苑。

今天小编为大家带来的主题是Matlab入门学习,

欢迎您的用心访问,

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

Share interest, spread happiness,

Increase your knowledge and leave something beautiful!

Dear you,

This is LearningYard Academy.

The topic that the editor brings to you today is Matlab introductory learning,

Welcome your visit,

This tweet takes about 5 minutes to read, please read it patiently.

元胞数组

Cell array

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

The cell array is a special data type of MATLAB. The cell array is regarded as an 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 ).

适用范围:

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

【实数】6;

【向量】[1 2 3];

【文本】LearningYard;

【矩阵】magic(4)。

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

Scope of application:


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


【Real Number】6;


【Vector】[1 2 3];


[Text] LearningYard;


[Matrix] magic(4).


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


代码表述:

新建脚本,我们采用大括号{ }进行cell数组的创建:


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


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


提取数组具体元素:

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

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

元胞数组的转化:

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

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

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

Extract specific elements of the array:

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}.

Conversion of cell array:

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

循环覆盖值累积

Cycle coverage value accumulation

假如在上一步骤将“end+1”删掉

我们能清楚地看到Best_value和Worst_value的数值随着语句的循环一直在发生更替


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

Note: 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.

行向量转化为矩阵

Convert row vector to matrix

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

The result encoded by the (end+1) instruction is in the form of a row vector. If we want to convert it into a matrix, we can understand the matrix as the accumulation of multiple row vectors of the same specification, which is expressed in code as:

[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进行赋值,使其变成矩阵的形式。


今天的分享就到这里了。

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

欢迎给我们留言,让我们相约明天,

祝您今天过得开心快乐!

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!

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“matlab如何给元胞赋值,matlab元胞数组赋值,matlab怎么给元胞数组赋值”边界阅读