1

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.

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
user2851655
  • 133
  • 2
  • 9
  • When nothing else works, read the manual/help: http://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html (don't forget to also check out "For more information" section) –  Oct 10 '14 at 08:50
  • Dear @Oleg Komarov Thanks for reply. i read it but i could not find what acctually i was looking for........... – user2851655 Oct 10 '14 at 08:58
  • 1
    @user2851655 see the end of the answer in the duplicate question. You just call `()` after `{}` as in `a{1}(1)` – Dan Oct 10 '14 at 09:00

2 Answers2

3

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

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
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.

Community
  • 1
  • 1
MeMyselfAndI
  • 1,320
  • 7
  • 12
  • 2
    In order to access elements of elements of a cell array, single-level indexing is not enough. `a{1,1}(1,1)` is rather the answer. –  Oct 10 '14 at 08:57
  • That is just how you interpret the question. Accessing the first value in the array that is in the first element of a cell is done like that. Anyway, you do have a point. – MeMyselfAndI Oct 10 '14 at 08:59
  • @Jan de Gier. Thanks a lot Dear and i really appriciate for your help. but i was looking for somthing like **Robert** ans.... THANKSSSSSSSSSSSSSSSSSSSSSSS – user2851655 Oct 10 '14 at 09:01