How we can combine two different series and add them
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Suppose we have two different sets of arithmetic series ranging from 1 to n. how to combine or add them together so that the elements are not repeated.
4 commentaires
Réponses (2)
John BG
le 4 Août 2017
1.
generating 2 sequences
p=randi([-10 10],1,12)
q=randi([-10 10],1,12)
p =
7 9 -8 9 3 -8 -5 1 10 10 -7 10
q =
10 0 6 -8 -2 9 6 10 3 -10 7 9
2.
calculating X
either by applying the first expression directly
p([1:2:end]).*q([2:2:end])
=
0 64 27 -50 -100 -63
X=sum(p([1:2:end]).*q([2:2:end]))
=
-122
or adding q(0)=0 just in case
q=[0 q]
X=sum(p([1:2:end]).*q([2:2:end]))
=
-122
same result
3.
calculating Y
Y=sum(p([2:2:end]).*q([1:2:end]))
=
266
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
0 commentaires
Jan
le 5 Août 2017
Modifié(e) : Jan
le 5 Août 2017
If you provide the input data and show what you have tried so far, posting an answer would require less guessing. I guess that p and q are vectors with n = length(p) / 2. Then what about:
X = 0;
for k = 2:2:n+1
X = X + p(k) * q(k-1);
end
Y = 0;
for c = 2:n
Y = p(2 * c) * q(2 * c - 1);
end
R = X + Y;
Note that I've shifted the indices by one, because they start at 1 in Matlab, not at 0, such that q(k-1) would fail for k=1. If this replies the wanted result, try:
R = sum(p(2:2:n+1) .* q(1:2:n)) + sum(p(2 * (2:n)) .* q(2 * (2:n) - 1))
or slightly faster:
R = sum(p(2:2:n+1) .* q(1:2:n)) + sum(p(4:2:2*n) .* q(4:2:2*n-1))
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!