Reputation: 1255
Imagine I have a cell array
A = {0, 1 ,2, 3, ...}
and indice vector
I = [0, 1, 0, 1, 0, ...]
and values
V = [2, 3]
and I want something like
A{I} = [A{I}; V]' = {0, [1 2], 2, [3 3], ....};
That is, I want to append several values to some cells of a cell array at once. How would I do that most elegantly/efficiently? :)
Upvotes: 4
Views: 132
Reputation: 114816
You can use cellfun
A(I==1) = cellfun( @(x,y) [x y], A(I==1), num2cell(V), 'UniformOutput', 0 );
Note the usage of regular subscripting (using ()
, rather than {}
) to index the chosen cell elements using I==1
. Also note that V
is passed as a cell array (using num2cell
) and not as a regular array.
Upvotes: 2