3

C is a cell consisting of some vectors:

C = {[1, 2], [2, 3]};

I want to read the first entry of the first vector in C. But I can not use the following:

C{1}[2]

I get the following error:

Error: Unbalanced or unexpected parenthesis or bracket.

How can I make it read the value?

1750028
  • 100
  • 10
jequesais
  • 137
  • 2
  • 4

1 Answers1

6

You can access individual elements of matrices in cell array like this:

C{n,m}(ii,jj);

This will give you element (ii,jj) of the matrix at index (n,m) of the cell array.

Hence, for your particular example,

val = C{1,1}(1,1) (or val = C{1}(1))

will assign the value of the first element of the first vector in the cell array to the variable val.

ThijsW
  • 2,599
  • 15
  • 17
  • 2
    It is better not to use [`i` and `j` as variables in Matlab](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab) – Shai Feb 21 '13 at 07:26
  • 1
    I know, but I can't help it, it's too much of a habit just using `i` and `j`. I changed it to the most (I think) commonly used alternatives, `ii` and `jj`. – ThijsW Feb 21 '13 at 07:31
  • I prefer to use 1i and 1j if the imaginary unit is needed as [shoelzer](http://stackoverflow.com/a/14861015/3058047) pointed out. In my opinion, it is the most readable and consistent solution to this issue – tc88 Nov 08 '16 at 13:13