4

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? :)

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.