How to store output vector from optimizations from a foor loop iterating through a matrix(columnwise)?
Show older comments
I have written an optimization function that returns a weightvector [Nx1]. Now I want to solve the optimization function (which I don't show because it is very complicated) for each of the column-vector in the matrix gamma. These vectors are used in the optimization function. Therefore, i loop trhough the gamma matrix and then want to store each resulting weight vector in a matrix. But that doesn't work because I can't index the matrix_weight with r, because r is a vector. How can I store the result of each optimization in the matrix? The loop works, the solution is just overwritten everytime.||
gamma = [1 2 3 4; 1 1 1 1; 8 1 9 7]';
matrix_weigths = zeros(N,size(gamma,2));
for r = gamma
weights = optimization(Returns, ExpRet, r)
matrix_weights = weights;
end
When I put
matrix_weights (:,r) = weights;
then the folling error occurs: Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts
Does anybody have an Idea how to solve this issue?
4 Comments
Rik
on 27 Feb 2018
What size is weights?
cluelessgirl
on 27 Feb 2018
Rik
on 27 Feb 2018
Then there is no reason matrix_weights(:,r)=weights; shouldn't work. You can try to find the bug with the debugger. Under the breakpoints button you can select 'pause on error' or 'stop on error'. Enable it and check if the dimensions of matrix_weights and weights are still as expected when the error occurs.
cluelessgirl
on 27 Feb 2018
Accepted Answer
More Answers (0)
Categories
Find more on Problem-Based Optimization Setup in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!