0

Given myCellArray{10,3} = [];, I would like to fill in the first column with consecutive numbers (let's say 1 through 10). I know I could do it like this:

[myCellArray{1:10,1}] = deal(1,2,3,4,5,6,7,8,9,10)
myCellArray = 
    [ 1]    []    []
    [ 2]    []    []
    [ 3]    []    []
    [ 4]    []    []
    [ 5]    []    []
    [ 6]    []    []
    [ 7]    []    []
    [ 8]    []    []
    [ 9]    []    []
    [10]    []    []

However, if my cell array is much larger (say 1,000 rows rather than 10), writing out the comma-separated values obviously becomes tedious:

[myCellArray{1:10,1}] = deal(1,2,3, ... ,1000)

Is there a way to create this comma-separated "list" of numbers automatically? Something like (1:10)? I know I could assign the values via loops, but is there an elegant one-line solution or something close to that?

1 Answer 1

3

This could be one way to do it with num2cell -

myCellArray(:,1) = num2cell(1:size(myCellArray,1))

In place of num2cell, you can use mat2cell which might not look very elegant though -

mat2cell([1:size(myCellArray,1)]',ones(1,size(myCellArray,1)),1)
Sign up to request clarification or add additional context in comments.

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.