How we can combine two different series and add them

조회 수: 5 (최근 30일)
phoenix
phoenix 2017년 8월 4일
편집: Jan 2017년 8월 5일
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