Create cell array or 3D matrix
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tomás Nunes
el 23 de Abr. de 2018
Comentada: Ameer Hamza
el 7 de Mayo de 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
3 comentarios
Bob Thompson
el 23 de Abr. de 2018
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'"
Respuesta aceptada
Ameer Hamza
el 23 de Abr. de 2018
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 comentarios
Ameer Hamza
el 7 de Mayo de 2018
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!