matlab distinguish {} and () of

In writing these days of Matlab, the use of an array of cellular and {} () operator, always during various problems, simply record it.

First look at the official explanation: You can use parentheses () for cellular array index, use curly braces {} for cellular content to index.

In other words when using a cellular data cellular resultant set, to use {} stored in ().

Macro cellular array is understood that the data may contain different types of data types, micro-array is understood that a cellular array of cells consisting of one element, wherein each cell can only contain the same type of data (an array of arrays or cellular) .

While () only to the cellular index set, can not obtain the data stored in the cellular; {} acquired data can be stored in the cellular.

For example short answer:

A = cell(2,2);
B = {'A' 'B';'C' 'D'};

 Cellular empty outset a 2 * 2 array A, and then declare a cellular 2 * 2 array which has an initial value, we want the first row of B assigned to the data A (1, 1).

 

If written in the following way,

A(1, 1) = B(1,:);

Look at the results:

 

 

 It appears to be wrong, analyze: A (1, 1) is a cellular index set, the result can only be one cell, and B itself cellular array, the first row B are two cellular using one cell to cell assignments being given two yuan, of course, to see the type of error guessing the problem is that the array dimensions.

It added the following written like this it?

A{1, 1} = B{1,:};

It appears to run without any errors

 

Look at the results:

 

 Why only one A result of it?

Analyze: B {1,:} for the index values ​​of all of the first row of B Cellular

 

 Respectively, which corresponds to the acquired value (1,1) and (1,2) position B, the two arrays are equivalent, but the (1,1) position A of the first array to obtain only Found ignored give less than a second.

Its wording should be similar (currently just my guess)

A{1, 1} = 'A';
'B'

第二个值 'B' 压根和A{1,1}产生不了任何关系。

 

正确的代码如下:

A{1, 1} = B(1,:);

没有任何错误,结果正确:

 

 

 分析一下:A{1, 1}为索引 A 元胞数组中(1,1)位置的内容,我们将 B 的第一行,使用 () 进行索引,相当于得到了一个 1 * 2 的元胞数组,我们将一个元胞数组赋值给了A的(1,1)的元胞。 

 

Guess you like

Origin www.cnblogs.com/HyattXia/p/12329616.html