Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

repeating same procedure for two data sets by cell or struct array?

1 Ansicht (letzte 30 Tage)
yp78
yp78 am 30 Mär. 2018
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
I'm struggling to find a way to efficiently repeat the same procedure for two separate data sets. I thought it is something to do with struct or cell arrays, but no idea how to do it. I would truly appreciate your help!
% both data are m by n matrix containing returns of stock prices
rtn1=data1;
rtn2=data2;
% % want to avoid repetition by indexing the data names
% I know it is wrong, but the image of what I would like to do is...
for j=1:2
% operation is done only for the first column of the data
firstRtn=rtn(j)(:,1);
% calculate 1 year average
rollingAve=zeros(length(firstRtn)-251,1);
for i=1:length(rtn(j))-251
rollingAve(i)=average(rtn(j)(i:i+251));
end
figure
plot(rollingAve)
end

Antworten (1)

Walter Roberson
Walter Roberson am 30 Mär. 2018
rtn = {data1; data2};
for j = 1 : length(rtn)
rollingAve = movavg( rtj{j}, 'simple', 251);
figure
plot(rollingAve)
end
  1 Kommentar
yp78
yp78 am 30 Mär. 2018
Bearbeitet: yp78 am 30 Mär. 2018
Thank you Walter, I got the idea of how to use the cell. But when I run the code(after modifying the typo), I have the following error message. "Operands to the and && operators must be convertible to logical scalar values." It seems to be related to the movavg function. But actually, I have to use similar operations for other statistics (some of them user defined functions) other than the moving average. Could you show me how I can access to the (i:i+251) elements in the cell array, looking at my first code posted?

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by