フィルターのクリア

How we can combine two different series and add them

1 回表示 (過去 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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by