0
votes

Hi I have an object A which contains 9 vectors all of size [1 3], three dimensional vectors. How do I sum all of these to create a new vector of size [1 3] i.e. how to I take the contribution of each vector, and sum each of their contributions to give me a final vector. Thanks in advance. My vectors are stored in a cell array. How A is defined:

 ri = Rix{1,1};
 rj = Riy{1,1};


 vec2 = @(i)[ri(i), rj(i), 0];
 A = arrayfun(vec2, reshape(1:numel(ri), size(ri)), 'UniformOutput', 0);

and this is what I have tried so far:

 B = cellfun(@(A)nansum(A(:))'un', 0);

with this error

??? b = cellfun(@(distance)nansum(distance(:))'un', 0); | Error: Unexpected MATLAB expression.

1
Show us the definition of A and what you have triedThor
cellfun is not the way to go here, because your resultis only one vector element, not several vectors. Like @Vidar suggested, cell2mat is a good way to solve this problem. Regardless to your question, however: you have a syntax error with cellfun. Read this documentation if you want to use it properly.Eitan T

1 Answers

3
votes

Is this what you are looking for?

dummy = [1 2 3];
A = {dummy;dummy;dummy;dummy;dummy;dummy;dummy;dummy;dummy}
Asum = sum(cell2mat(A));

Result:

Asum = [9 18 27]

As you can see, cell2mat is the trick here.