views:

263

answers:

1

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].

+6  A: 

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([]);
gnovice
woohoo! gnovice comes through again. we're going to have to call you "gexpert"
Jason S
I have to become "gintermediate" first. ;)
gnovice