How do I sum multiple vectors inside an array?
Ältere Kommentare anzeigen
I am trying to sum vectors for specific columns of an array using cell arrays but it is summing all the components of each vector and is returning one value which is the problem. I want it to sum as vectors should with x components and y components and return a vector.
I am trying to iterate this in a loop for each column so if there is also a way without indexing by just using a loop, feel free to let me know. Thanks in advance!
4 Kommentare
KALYAN ACHARJYA
am 9 Mai 2019
Bearbeitet: KALYAN ACHARJYA
am 9 Mai 2019
Can you elaborate with example and what result you are expecting?
Aaron Rapliza
am 9 Mai 2019
Bearbeitet: Aaron Rapliza
am 9 Mai 2019
Aaron Rapliza
am 9 Mai 2019
Bearbeitet: Jan
am 9 Mai 2019
Antworten (1)
You can use the force between two bodies twice:
f = zeros(n, n, 3);
for i = 1:n
for x = i+1:n
f_ix = G * (p(x,:) - p(i,:)) * ...
((mass(i) * mass(x)) / (norm(p(x,:) - p(i,:))^3))
f(i,x,:) = f_ix;
f(x,i,:) = -f_ix;
end
end
% Calculate the total acceleration *after* getting all forces:
a = sum(f, 3) ./ mass; % With auto-expanding since R2016b
Kategorien
Mehr zu Computational Geometry finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!