Effacer les filtres
Effacer les filtres

How we can combine two different series and add them

1 vue (au cours des 30 derniers jours)
phoenix
phoenix le 4 Août 2017
Modifié(e) : Jan le 5 Août 2017
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
KSSV
KSSV le 4 Août 2017
That's what...it would be a number...adding numbers is not a deal...
Cong Ba
Cong Ba le 4 Août 2017
Could you provide an input&output pair so people understand what you expect?

Connectez-vous pour commenter.

Réponses (2)

John BG
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

Jan
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))

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by