How we can combine two different series and add them

5 次查看(过去 30 天)
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 个评论
KSSV
KSSV 2017-8-4
That's what...it would be a number...adding numbers is not a deal...
Cong Ba
Cong Ba 2017-8-4
Could you provide an input&output pair so people understand what you expect?

请先登录,再进行评论。

回答(2 个)

John BG
John BG 2017-8-4
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 2017-8-5
编辑:Jan 2017-8-5
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))

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by