Home > Software engineering >  How to append an empty array to a cell array
How to append an empty array to a cell array

Time:03-29

How do you append an empty array to a (non-empty) cell array?

For example, starting with

c={[1],[2]}

desire

c={[1],[2],[]}

Concatenation would remove the empty array whether it is double, char or cell.

CodePudding user response:

You can just append it by using end 1:

c={[1],[2]}
c = 
    [1]    [2]
c{end 1} = []  % end 1 "appends"
c = 
    [1]    [2]    []

MATLAB note: appending is usually used as a way to grow an array in size within a loop, which is not recommended in MATLAB. Instead, use preallocation to initially apply the final size whenever possible.

  • Related