How to Access the Cell array Matrix? [duplicate]

限于喜欢 提交于 2019-12-11 09:39:18

问题


Could any one tell me about the indexing of a cell Array? I've tried to google it but I could only find unsatisfied result (may be I'm not good in googling). For matrix indexing I found a good document which can be found here. For my case let take a simple example.

a = {ones(10)}

and I want to access the first element of a. Something like

a(1,1) % this will give a 10 x 10 matrix but i am not looking for it.

I can do it by changing it into a matrix like

a = cell2mat(a);
a(1,1)
ans = 1

but is there any direct way to access first element of cell array sub matrix.


回答1:


To access the first element of a the first cell in a cell array, you may do:

a = {ones(10)};
a{1}(1)

If you have multidimensional cell arrays, with multidimensional numerical arrays inside it, you can do:

a{2,3}(4,5)

This will give you element (4,5) of cell (2,3).




回答2:


You actually are accessing the first element of a, and it contains a matrix of 10×10 filled with ones.

Initializing a cell can be done by a = cell(10), and obtaining a certain value of the cell matrix is done with a{i,j}. See also the documentation.


For initializing a cell array with some values, see this question.



来源:https://stackoverflow.com/questions/26295402/how-to-access-the-cell-array-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!