Populate cell array with regression residuals using for loop and if statement

1 view (last 30 days)
Tomás Nunes on 19 Apr 2018

I am trying to populate a 125x32 cell array with regression residuals over previous 30-day returns. I have a matrix called sig1 that indicates that I should perform the regression every time there is a 1 or -1. The returns are in a matrix called ret, and they all have the same dimensions. However, I have been hitting some errors related to dimensions and number of elements in each side of the assignment. Basically I want to have a 30*1 matrix within each cell of C, every time there is a 1 or -1 in sig1. This is what I am trying:

for j = 1:1:32
for i = 31:1:125
if sig1(i,j) == 1
[b1(i,j),bint,R1(i,j)] = regress(ret(i-30:i-1,j), [ones(30,1) mkt1(i-30:i-1)]);
C{i,j} = R1(i,j);
elseif sig1(i,j) == -1
[b1(i,j),bint,R1(i,j)] = regress(ret(i-30:i-1,j), [ones(30,1) mkt1(i-30:i-1)]);
C{i,j} = R1(i,j);
else
C{i,j} = 0;
end
end
end

Every time the sig1 matrix is a 0 (not 1 nor -1), the cell array C is supposed to be zero. i feel like there is some mistake with the [b1(i,j),bint,R1(i,j)] part, but I've tried different approaches and none of them works. When I run the for loop it gives me the following error: "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 2-by-1."

Also, I have tried to put the residuals in a 125x32x30 matrix instead of the cell array, but it didn't work as well.

Finally, I have tried to reduce the matrices just to the first column in order to unbug this, but I didn't succeed. Any thoughts? Thanks