How do I sum many vectors together by using 'for' loop?
Show older comments
Hi,
I am currently working to find the center of a few random points. My idea is to take the sum of all the points and find the average. I have tried to use a simple 3D example of adding two vectors together by using the 'for' loop.
pos_swarm = zeros(3,s);
pos_swarm(:,1) = [0,0,0];
pos_swarm(:,2) = [5,5,5];
sum = zeros(3,s);
for i = 1:s
sum(:,i) = pos_swarm(:,1) + pos_swarm(:,2);
average = sum ./ s
end
How do I add 'n' number of vectors together? Greatly appreciate if anyone can help me with this. :)
Regards, Thomas
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!