3

I've been using Matlab for quite awhile but this one has got me--I want a one-liner that will take an array and write it into the indexed entries of a cell, e.g.

>> c = cell(1,6);
>> b = [1 2 3];
>> c{[2 4 6]} = b; %This doesn't actually work
>> disp(c)

   []  [1]  []  [2]  []  [3]

I've tried all sorts of versions of this with num2cell, deal et al. but I can't find the magic bullet. Can it be done in a single assignment?

1 Answer 1

3

With mat2cell -

c([2 4 6]) = mat2cell(b,1,ones(1,numel(b))); 

With num2cell -

c([2 4 6]) = num2cell(b);

Output -

>> disp(c)
    []    [1]    []    [2]    []    [3]
Sign up to request clarification or add additional context in comments.

1 Comment

@MattPhillips Yup! That was missing there :)

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.