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.

3
  • 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} Commented May 13, 2015 at 7: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 Commented May 13, 2015 at 7:40
  • 1
    oh, just use size then Commented May 13, 2015 at 8:15

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

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 size(a,1) given you end of a{end}, yes.
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 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;.
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?
2

You can use size():

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

index = size(a{1},2)+1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.