0

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. 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));- 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. funds is a ~2500x8 doubles matrix.

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
n1k31t4
  • 2,745
  • 2
  • 24
  • 38
  • try `uint32(count-frame(j)+1)` instead of `count-frame(j)+1` – Tae-Sung Shin Apr 01 '14 at 16:40
  • It didn't work - thanks anyway. – n1k31t4 Apr 01 '14 at 18:58
  • Is that `mean` meant to be the function, `mean`? Because you're overwriting it with a variable earlier in the code. – nkjt Apr 01 '14 at 19:59
  • It is badly used, true - i have now changed it to just `avg`, however I had already checked it early on to see if it interfered, but it didn't and now with the change made everywhere i am still getting the same error message - good spot though, thanks! I have since found this link - http://stackoverflow.com/questions/466972/array-of-matrices-in-matlab?rq=1 - which has a similar problem, and shows my indexing should be correct - so it must be that the subscript indices are not being recognised as numbers... – n1k31t4 Apr 01 '14 at 20:05
  • It does not work because you are subscripting the matrix `mean` with real the real values in `fund(count-frame(j)+1:count)`. Subscripts have to be as the error message describes them. – Drake Apr 01 '14 at 20:35
  • Do I understand correctly, that because I had used `mean` as a variable name (I know that was stupid!), it was no longer functioning as a function at all? – n1k31t4 Apr 01 '14 at 22:12

1 Answers1

0

As mentioned in the comments, the problem occurs as a value is assigned to 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.

Community
  • 1
  • 1
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122