How to create for-loop for this case ?

4 ビュー (過去 30 日間)
Hannes Arnar
Hannes Arnar 2020 年 1 月 28 日
コメント済み: Hannes Arnar 2020 年 1 月 29 日
How would you create a for-loop for this case, I want to make the S and C and R jump on 25 days
close all; clear all; clc;
data1 = xlsread('dataCompanyprices','Sheet1','W4:AP1250');
S = std(data1)*sqrt(252); %Standard deviation for assets
C = cov(data1)*252; %Annual Covariance
r = mean(data1)*252; %Annual asset return
e = ones(1,20); %Unit vector
  2 件のコメント
Geoff Hayes
Geoff Hayes 2020 年 1 月 28 日
Hannes - what do you mean by jump on 25 days? Where would the 0, 25, 50, 75, etc. fit into the above code? In the data1 array?
Hannes Arnar
Hannes Arnar 2020 年 1 月 28 日
Yes, you can see the xlx file here.

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

回答 (1 件)

Paresh yeole
Paresh yeole 2020 年 1 月 28 日
Referring from your previous question,
I think this is what you need :
for i=1:(no_of_datapoints/25)
S(i) = std(data1(1+(i-1)*25:i*25))*sqrt(252); %Standard deviation for assets
C(i) = cov(data1(1+(i-1)*25:i*25))*252; %Annual Covariance
r(i) = mean(data1(1+(i-1)*25:i*25))*252;
end
  3 件のコメント
Paresh yeole
Paresh yeole 2020 年 1 月 28 日
My solution is for a 1-D array of data points. If you have multiple columns, then you need an extra for loop. If I understand correctly,
for j=1:no_of_columns
%rep
data = data1(:,j);
for i=1:(no_of_datapoints/25)
S(i,j) = std(data(1+(i-1)*25:i*25))*sqrt(252); %Standard deviation for assets
C(i,j) = cov(data(1+(i-1)*25:i*25))*252; %Annual Covariance
r(i,j) = mean(data(1+(i-1)*25:i*25))*252;
% here you will get a matrix of all the three values which correspond to set of 25 points
% for every column
end
end
Hannes Arnar
Hannes Arnar 2020 年 1 月 29 日
Ok, I just want to take out of the data set 25 lines in each iteration, I put that in a new array.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by