MATLAB中单元格索引的意义是什么 [英] What is the point of cell indexing in MATLAB

查看:178
本文介绍了MATLAB中单元格索引的意义是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

索引的要点主要是在MATLAB中获取值, 对于单元格数组,有内容索引,因此单元格索引 仅用于从单元格数组中选择子集吗?

The point of indexing is mainly to get the value, in MATLAB, for cell array, there is content indexing, so cell indexing is only for selecting subset from cell array right?

还有其他高级用法吗?喜欢用作 指针并传递给函数?

is there anything other advanced usage for it? like using as a pointer and pass to function?

推荐答案

有一个非常简化的答案. {} -indexing返回内容,()-indexing创建带有索引元素的子单元格.让我们举一个简单的例子:

There is a heavily simplified answer. {}-indexing returns you the content, ()-indexing creates a subcell with the indexed elements. Let's take a simple example:

>> a=x(2)

a = 

    [2]

>> class(a)

ans =

cell

>> b=x{2}

b =

     2

>> class(b)

ans =

double

现在继续使用非标量元素.对于()索引,一切工作均符合预期,您将收到一个包含以下元素的子单元:

Now continue with non-scalar elements. For the ()-indexing everything behaves as expected, you receive a subcell with the elements:

>> a=x(2:3)

a = 

    [2]    [3]

Matlab真正特别的事情是使用{}-索引和非标量索引.它会返回一个逗号分隔列表,其中包含所有内容.现在这里发生了什么:

The thing really special to Matlab is using {}-indexing with non-scalar indices. It returns a Comma-Separated List with all the contents. Now what is happening here:

>> b=x{2:3}

b =

     2

逗号分隔列表的行为类似于具有两个返回参数的函数.您只需要一个值,只分配了一个值.第二个值丢失.您还可以使用此功能一次将多个元素分配给单个列表:

The Comma-Separated List behaves similar to a function with two return arguments. You want only one value, only one value is assigned. The second value is lost. You can also use this to assign multiple elements to individual lists at once:

>> [a,b]=x{2:3} %old MATLAB versions require deal here

a =

     2


b =

     3

现在终于有了一个非常强大的用逗号分隔列表的用例.假设您有一些愚蠢的函数foo,它需要许多输入参数.在您的代码中,您可以编写如下内容:

Now finally to a very powerful use case of comma separated lists. Assume you have some stupid function foo which requires many input arguments. In your code you could write something like:

foo(a,b,c,d,e,f)

或者,假设您已将所有参数存储在单元格中:

Or, assuming you have all parameters stored in a cell:

foo(a{1},a{2},a{3},a{4},a{5},a{6})

或者,您可以使用逗号分隔的列表来调用该函数.假设a有6个元素,则此行与上一行完全等效:

Alternatively you can call the function using a comma separated list. Assuming a has 6 elements, this line is fully equivalent to the previous:

foo(a{:}) %The : is a short cut for 1:end, index the first to the last element

此处为输入参数说明的相同技术也可以用于输出参数.

The same technique demonstrated here for input arguments can also be used for output arguments.

关于您关于指针的最后一个问题. Matlab不使用指针,也没有对其进行补充(oop Matlab中的handle除外),但是Matlab在优化内存使用方面非常强大.尤其是使用写入时复制,在大多数情况下无需使用指针.通常,您最终会得到类似

Regarding your final question about pointers. Matlab does not use pointers and it has no supplement for it (except handle in oop Matlab), but Matlab is very strong in optimizing the memory usage. Especially using Copy-on-write makes it unnecessary to have pointers in most cases. You typically end up with functions like

M=myMatrixOperation(M,parameter,parameter2)

在何处输入数据并返回.

Where you input your data and return it.

这篇关于MATLAB中单元格索引的意义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆