1
votes

[EDIT]

Say, I have a cell array A consisting of n cells and each of them is a zero vector of size (m,1).

Example: A is a 1x2 cell array A = [100x1 double] [100x1 double]

How can I insert new values to each cells, on the same rows and at the same time without having to write separate lines of code? Can I use cellfun to do so?

For instance, how can I replace the ithrow of the first cell with the new value m and the ithrow of the second cell with the new value n, using a single line of code?

m and n may or may not be the same values.

1
I don't understand, are the cell contents always vectors, Mx1, or are they matrices, MxN? Also, in your question, do you want to assign m to one cell and n to the other one, or two separate columns in row i? - Trogdor
Please provide a minimal reproducible example. Are you trying to replace existing rows, add new rows in the middle of each vector, or something else? - beaker
@Trogdor I will make the clarifications - nashynash
@beaker Let me clarify things in the main question space. - nashynash
@nashynash That would be the place to do it. And now that I've seen your previous question, I have to ask why you've chosen not to use a multidimensional array rather than a cell array (assuming the matrices are all the same size, of course). - beaker

1 Answers

0
votes

as in comments noted you may try other options such as array for its efficiency, however as an answer to the question you can use evalin function and using change_cell you can for example change contents of second row fo each cell to the vals just in one line of code!:

A = {[1 2 3],[4 5 6]};
change_cell = @(rownum , vals) evalin(...
                                    'base', ...
                                    [...
                                        'rownum =', int2str(rownum),';',...
                                        'vals=[', num2str(vals),'];',...
                                        'for i = 1:length(A);',...
                                            'A{i}(rownum)=vals(i);',...
                                        'end;'...
                                    ]);
change_cell(2, [6 8])