How to calculate reveral summations

1 回表示 (過去 30 日間)
Xuefei Sun
Xuefei Sun 2021 年 2 月 23 日
編集済み: Matt J 2021 年 2 月 25 日
I need to calculate sigma=sum(u(t)*u(t+a)), from a to 600(t=a to 600), and a is from 1 to 600. So I should get 600 different sigma.
How to do it?
  3 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 24 日
Is it correct that u is least length 600+h, so that u(t+h) can be in range for indexing when t becomes 600 ?
Xuefei Sun
Xuefei Sun 2021 年 2 月 24 日
Sorry, the "h" should be a.
The data is u. For example u1 to u1500. And when a=1, I will get sigma(1)=u1*u(1+1)+u(2)*u(2+1)+...u(600)*u(600+1), and then a=2, I will get sigma(2)=u2*u(2+2)+u(3)*u(3+2)+...u(600)*u(600+2). ... a=600, sigma(600)=u600*u(600+600)

サインインしてコメントする。

回答 (2 件)

Matt J
Matt J 2021 年 2 月 23 日
Use conv() or xcorr().
  8 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 24 日
Y = circshift(A,K) circularly shifts the elements in array A by K positions. If K is an integer, then circshift shifts along the first dimension of A whose size does not equal 1. If K is a vector of integers, then each element of K indicates the shift amount in the corresponding dimension of A.
I don't think you have 600 dimensions to your u .
If you did not get an error when you did the xcorr() then that implies that u and y must both have been vectors.
The example in doc xcorr for Cross-Correlation of Two Vectors shows a vector of length 16 correlated with a modified version of itself. The output is length 31, reflecting a lag up to (16-1) before, and a lag up to (16-1) after, for a total of (16-1) + 1 + (16-1) = 2*16-1 .
Therefore for a vector of length 600, you should expect a length of 2*600-1 = 1199 for the output.
Xuefei Sun
Xuefei Sun 2021 年 2 月 24 日
Yes, I think that is my problem, since my u data is 600x1, my y data is also 600x1. Both of them are Vrctors.
So how to fix it?

サインインしてコメントする。


Matt J
Matt J 2021 年 2 月 24 日
編集済み: Matt J 2021 年 2 月 24 日
T=triu(ones(600));
I=(1:600).' + (1:600);
ur=u(:).';
sigma= (T.*ur(I))*ur(1:600).';
  2 件のコメント
Xuefei Sun
Xuefei Sun 2021 年 2 月 24 日
It won't use xcorr() anymore? But when I type this code, it said: "Index exceeds the number of array elements (600)"
Matt J
Matt J 2021 年 2 月 25 日
編集済み: Matt J 2021 年 2 月 25 日
Works fine when I run it.
u=rand(1,1500); %Example data
T=triu(ones(600));
I=(1:600).' + (1:600);
ur=u(:).';
sigma= (T.*ur(I))*ur(1:600).';
whos sigma
Name Size Bytes Class Attributes sigma 600x1 4800 double

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by