In an assignment A(:) = B, the number of elements in A and B must be the same.
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
When trying to make a regression function by using a function that is manually created, I have this error alerting: In an assignment A(:) = B, the number of elements in A and B must be the same.
The code I have are included below(ols is a function created by me) and I am pretty sure it is no the problem in the function.
% code
for i=1:11;
[estimates(i), error(i), estvar(i), stderror(i),Tvalues(i),R2(i),R2_adj(i)]=ols(rt(2:end,i),rt(1:end-1,i),0);
estimates_all=[estimates_all estimates(i)];
error_all=[error_all error(i)];
estimates_all=[estvar_all estvar(i)];
stderror_all=[stderror_all stderror(i)];
Tvalues_all=[Tvalues_all Tvalues(i)];
R2_all=[R2_all R2(i)];
R2_adj_all=[R2_adj_all R2_adj(i)];
end
1 commentaire
Robert
le 6 Oct 2016
Separate from my answer, I recommend you re-think your storage variables. The way you index into estimates and the others looks like your are creating an array in which to store the results, but then you also append those results to a different array. You don't need to do both and saving them with indices is much better because it allows you to pre-allocate the array.
What you have now is equivalent to
result_all = [];
for ii = 1:3
result(ii) = ii;
end
disp(result_all)
for which the response is
[ 1 1 2 1 2 3 ]
What you really want is more like
N = 3;
result = nan(1,N);
for ii = 1:N
result(ii) = ii;
end
disp(result)
which yields
[ 1 2 3 ]
You can shorten this by reversing the order of the loop. That way, the first pass through the loop makes the full size array results and MATLAB doesn't spend time resizing the array each time. In the prior example we achieved the same result by pre-allocating with nan.
for ii = 3:-1:1
result(ii) = ii;
end
disp(result)
which also yields
[ 1 2 3 ]
Following this, your code would become something like
for ii=11:-1:1;
[estimates(ii), error(ii), estvar(ii),...
stderror(ii),Tvalues(ii),R2(ii),R2_adj(ii)]=...
ols(rt(2:end,i),rt(1:end-1,i),0);
end
Réponses (1)
Robert
le 6 Oct 2016
It looks like one of the outputs of your function ols is not a scalar. The error you are seeing is the same as if you executed
a(1) = [1,1]
1 commentaire
Walter Roberson
le 6 Oct 2016
Note: the error can also occur if you try to store an empty array into a definite location, such as
b = [];
a(1) = b;
The empty array needs size 0 to store but a(1) is size 1.
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!