简体   繁体   English

为单元格数组中的矩阵分配值-MATLAB

[英]Assigning values to matrices in cell-arrays - MATLAB

I am trying to assign values to a matrix, which is an array element directly while going through a loop. 我试图将值分配给矩阵,矩阵在循环时直接是数组元素。 I am being told however that Subscript indices must either be real positive integers or logicals. 有人告诉我,下Subscript indices must either be real positive integers or logicals. But as you can see from the script below, I have read the output for each of the indices before they are actually used and each of them is real positive integer. 但是从下面的脚本中可以看到,在实际使用每个索引之前,我已经阅读了它们的输出,并且每个索引都是实数正整数。

The error is produced by the first loop statement fundMoments{1,i}((count-frame(j)+1), meanCol) = mean(fund(count-frame(j)+1:count)); 该错误是由第一个循环语句fundMoments{1,i}((count-frame(j)+1), meanCol) = mean(fund(count-frame(j)+1:count)); - I have run the left side of the line alone with the inputs defined as in the script and it tells me correctly that the value held in the matrix, in the array, is zero. -我只用脚本中定义的输入运行了行的左侧,它正确地告诉我数组中矩阵中保存的值为零。 However the complete line does not run. 但是,完整的行无法运行。

Any ideas where I am going wrong here? 有什么想法我在这里出错吗?

For some context to the code - I am trying to calculate moving averages (but not only the moving mean, but also the moving std. deviation and so on). 对于代码的某些上下文-我试图计算移动平均值(但不仅要计算移动平均值,还要计算移动标准偏差等)。 The value of frame is the length of the moving time-window being taken into account. frame的值是所考虑的移动时间窗口的长度。 funds is a ~2500x8 doubles matrix. funds是2500x8的双打矩阵。

frame = [252 504 756];   % the time frame to be used for moving averages
funds = funds_ROR;   %matrix with the returns of all funds
meanCol = 1;
st_devCol = 2;
skewCol = 3;
kurtCol = 4;

%for f = 1:length(frame)
mean = zeros(length(funds) - frame(1), 1);
st_dev = zeros(length(funds) - frame(1), 1);
skew = zeros(length(funds) - frame(1), 1);
kurt = zeros(length(funds) - frame(1), 1);

vectLength = length(mean);    % all the four vectors must be of the same length, so we only need use one

fundMoments = {[mean st_dev skew kurt], [mean st_dev skew kurt], [mean st_dev skew kurt], [mean st_dev skew kurt], [mean st_dev skew kurt], [mean st_dev skew kurt], [mean st_dev skew kurt], [mean st_dev skew kurt]};

for i = 1:1:8
    fund = funds_ROR(:,i);


    for j = 1:length(frame)

        for count = frame(j):length(fund)
            fund
            count
            frame_j = frame(j)
            meanCol
            fund(count-frame(j)+1:count)

            fundMoments{1,i}((count-frame(j)+1), meanCol) = mean(fund(count-frame(j)+1:count));
            fundMoments{1,i}(count-frame(j)+1, st_devCol) = std(fund(count-frame(j)+1:count));
            fundMoments{1,i}(count-frame(j)+1, skewCol) = mean(fund(count-frame(j)+1:count));
            fundMoments{1,i}(count-frame(j)+1, kurtCol) = mean(fund(count-frame(j)+1:count));

            % fund_kurt = kurtosis(1:1:frame(j));

            %                 date = time_series(frame(j):end);
            %                 figure(frame(j))
            %                 plot(date, fundMoments, date, fund_kurt)
            %                 header = ['Skew and Kurtosis - ', frame, ' moving average'];
            %                 title(header)
            %                 legend('Skew', 'Kurtsosis')
            %                 set(gcf, 'YLim', max(max(fundMoments), max(fund_kurt)))

        end
    end

end

As mentioned in the comments, the problem occurs as a value is assigned to mean . 如评论中所述,由于将值分配给mean ,因此出现问题。

Afterwards it is a variable but asker expects it to still be a function. 之后,它是一个变量,但问问者期望它仍然是一个函数。

I believe this would also have been found via the generic solution to this problem , from this question . 我相信,通过该问题通用解决方案也可以找到该问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM