Create cell array or 3D matrix

10 ビュー (過去 30 日間)
Tomás Nunes
Tomás Nunes 2018 年 4 月 23 日
コメント済み: Ameer Hamza 2018 年 5 月 7 日
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 件のコメント
Tomás Nunes
Tomás Nunes 2018 年 4 月 23 日
I believe the error comes from the third or fourth line, because when I changed the sig1 to a different matrix (7567x32), it didn't give me that error, but still couldn't perform the loop the way I want it. In fact, sig1 is just the first 125 rows of the larger matrix.
Bob Thompson
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'"

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 4 月 23 日
You are getting this error because you second input X to regress() is 30*2, therefore the first output from regress will be 2*1 but you are trying to assign it to a single element b1(i, j). Also since R1 is a cell matrix, you will have to change it on the left-hand side of the assignment like R{i,j}. For correction of b1 dimensions, based on your preference you might want to assign the value of b to a cell type variable like this
[b1{i,j},bint,R1{i,j}] = regress(ret(i-30:i-1,j), [ones(30,1) mkt1(i-30:i-1)]);
of course in above case, you will need to initialize b as cell. Another solution is to double the number of rows in matrix b and do something like
[b1(i:i+1,j),bint,R1{i,j}] = regress(ret(i-30:i-1,j), [ones(30,1) mkt1(i-30:i-1)]);
  4 件のコメント
Tomás Nunes
Tomás Nunes 2018 年 5 月 7 日
I just understood what was wrong with this loop. Apparently it made no sense to specify the (i,j) for the b and R1. So, if I just put [b,bint,R1] = ..., it gives me the results I needed. It was dumb to do it though..
Thanks a lot for the time spent on this!
Ameer Hamza
Ameer Hamza 2018 年 5 月 7 日
You are welcome. If it works for you then great. But note that it will only save values from the last iteration of for loops. All previous values will be overwritten.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by