Create cell array or 3D matrix
显示 更早的评论
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
3 个评论
Bob Thompson
2018-4-23
Which line is giving you the error message?
Tomás Nunes
2018-4-23
Bob Thompson
2018-4-23
The error message should tell you which line it occurs on, i.e.
"Unable to perform assignment because size of left side is 1-by-1 and size of right side is 2-by-1.
Error occurs on line 127 of myscript.m:
'[b1(i,j),bint,R1(i,j)] = regress(ret(i-30:i-1,j), [ones(30,1) mkt1'"
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!