3

I want to manipulate a cell array and make certain indices of the cell array contain the empty matrix []. I can't seem to figure out how to do this:

>> yy=num2cell(1:10)

yy = 

  [1]    [2]    [3]    [4]    [5]    [6]    [7]    [8]    [9]    [10]

>> yy{1:2:end}=[]
??? The right hand side of this assignment has too few values to satisfy
 the left hand side.
>> yy(1:2:end) = []

yy = 

  [2]    [4]    [6]    [8]    [10]

Bah! Can't seem to do what I want. I want to leave empty elements in the cell array, e.g.

  []    [2]    []    [4]    []    [6]    []    [8]    []    [10]

Any suggestions? My index vector could be arbitrary, and either in index form or boolean form, not necessarily [1 3 5 7 9].

1 Answer 1

7

What you can do is index the cell array (not the contents) using () and change each cell to an empty cell {[]}:

yy(1:2:end) = {[]};

An alternative is to use the DEAL function, but it looks a bit uglier:

[yy{1:2:end}] = deal([]);
Sign up to request clarification or add additional context in comments.

2 Comments

woohoo! gnovice comes through again. we're going to have to call you "gexpert"
I have to become "gintermediate" first. ;)

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.