How to assign an empty matrix to elements of an array of cells in MATLAB?

I want to manipulate an array of cells and make certain indices of the array of cells contain an empty matrix []. I cannot 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! I can't seem to do what I want. I want to leave empty elements in an array of cells, e.g.

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

Any suggestions? My index vector can be arbitrary and either in index form or in the form of a Boolean, not necessarily [1 3 5 7 9].

+3
source share
1 answer

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

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

DEAL, :

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

Source: https://habr.com/ru/post/1725652/


All Articles