Sum two cells and write to a third cell
62 views (last 30 days)
Show older comments
I have three cells A{10}, B{10} and C{10} of size 5x5. On each cell I want to do: A(:,1) = B(:,1) + C(:,1) I might also want to do: A(2:4,5) = B(2:4,5) + C(2:4,5)
Currently if I just write something like above, I will get error as Index exceeds matrix dimensions.
Any ideas on how this can be done (as fast as possible)
0 Comments
Accepted Answer
Alfonso Nieto-Castanon
on 11 May 2015
Edited: Alfonso Nieto-Castanon
on 11 May 2015
Probaby a loop is going to be fastest, e.g.:
for n=1:numel(A)
A{n}(:,1)=B{n}(:,1)+C{n}(:,1);
end
but if you really dislike for loops you could always do something like:
A = cellfun(@(a,b,c)[b(:,1)+c(:,1) a(:,2:end)], A, B, C, 'uni',0);
More Answers (1)
Walter Roberson
on 11 May 2015
Edited: Walter Roberson
on 12 May 2015
cellfun()
However, the method to do it "as fast as possible" is likely to involve creating some mex code written in C and optimizing the heck out of it. It would probably take you a few months to learn how to do properly -- I know the resident expert James Tursa is still learning about using mex after what must be at least 8 years of working with it.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!