1

How can I know the index of last item in cell array? e.g. I'd like to add an item to end of an cell array so I use

    a{1}(1,end+1) = 1

now I'd like to know what is the index equivalent to "end+1" in that statement?

any help is appreciated.

Shai
  • 111,146
  • 38
  • 238
  • 371
hamideh
  • 225
  • 1
  • 2
  • 12
  • To the end of a cell array (i.e. what your words imply you want to do) or to the end of a numerical array stored in a single cell of a cell array (i.e. what your code implies you're trying to do)? For the first case it's just going to be `a{end+1} = 1` or `a(end + 1) = {1}` – Dan May 13 '15 at 07:20
  • In both cases, I'd like to know what is the value of "end". For example a{1}(1,1)=1; a{1}(1,2)=1 is stored but i'm not aware of it. I just want to add the item to the end so I use e.g. a{1}(1,end+1) = 2 but for some other purpose i'd like to know how many numerical array is stored in that single item; i.e. i'd like to know id which matlab assigns to end. thanks – hamideh May 13 '15 at 07:40
  • 1
    oh, just use `size` then – Dan May 13 '15 at 08:15

2 Answers2

4

end simply stand for the size of the variable at the corresponding dimension

whatIsEnd = size( a{1}, 2 ); %// size along second dim

Therefore, end+1 is whatIsEnd+1.

If cell-array a has many elements and you wish to know the end of each and every one of them, you may consider using cellfun:

whatIsEnd = cellfun( @(x) size(x,2), a );

Important Note:
You are adding an element after the end of an array (location end+1). While this code works fine, it is not advisable, as you are changing the array size and this might incur performance depredation if not done with care.
You can read more about changing size of matlab arrays and pre-allocation in this thread

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thank you so much, it works for the numerical array in the cell, Now for the cell itself I think I should use whatIsEnd = size( a, 1), right? – hamideh May 13 '15 at 09:24
  • 1
    @hamideh `size(a,1)` given you `end` of `a{end}`, yes. – Shai May 13 '15 at 09:48
  • Yes, I'm changing the size of cell-array in each iteration. In fact my code was too slow when I was using array of structure for community detection and now I'm using cell-array in the hope of speed (having multiple attribute I though I couldnt use matrix) each time I discover a new community, I add another element to cell. according to your recommended page, it is not an effective approach So, should I use maximum number of clusters I guess for preallocation of my cluster cell and then use index = find(~cellfun('isempty',a),1,'last'); to find where should I insert new community members? Thx – hamideh May 13 '15 at 10:42
  • 1
    @hamideh you can keep a counter of how many valid clusters you have and then add the new cluster at `a{counter+1}` followed by `counter=counter+1;`. – Shai May 13 '15 at 10:50
  • Thanks, I was doing the same, but doesn't it degrade performance. i.e. it doesnt entail copying all items of cell to new cell (which has one more cell item) as in the case of matrix discussed in your recommended help page? I'd like to have higher performance. If it just add the new space to the end of previous cell, then I think there's no need for preallocation. what's your idea please? – hamideh May 13 '15 at 11:24
2

You can use size():

a = cell(1);
a{1} = [1,2,3];

index = size(a{1},2)+1;
Steffen
  • 2,381
  • 4
  • 20
  • 33