Optimize vector position and values to form another vector.
4 views (last 30 days)
Hi, I am stuck with a problem which I do not know how to proceed. I have 2 vectors:
a = [1 2 3 2 1];
b = zeros(1, 11);
I want to find the best placement (maximum 3 or 4) and weighting of vector 'a' inside vector 'b' so that the values of vector b have the least standard deviation possible.
A possible solution could be:
b(1:5) = a * 2;
b(4:8) = a * 1.5;
b(6:10) = a * 1.5;
b(9: 11) = a * 2;
However, if it were that simple to figure, I wouldn't need help :-). Please, leave a suggestion as to how this could be tackled. Thank you.
Andrew Fowler on 3 Jun 2011
You could set up your problem in a couple of ways. The easiest would be by brute force using nested loops, though it may not be the best:
a = [1 2 3 2 1]
b = zeros(1,11)
% Make a guess that you know is too high for the standard deviation
devb = 50
% There's only 7 places that a can go into b, so we make
% a for loop that has 7 increments.
for i = 1:7
b(i:i+4) = a; % You dimensions have to match. See what I did?
% The syntax here is inital_value:increment:final_value,
% and you get a vector that goes from initital_value
% to final_value by steps of increment.
for j = 1:0.1:2
c = b*j; % This is weighting as you presented it in your question,
% but I think it might not be right.
% if the values that have been run through the loops give
% you a lower deviation value than the others, this statement
% will overwrite the previous value of deviationb with the current
% value, and save the values of i and j so you know what your
% solution was.
if std(c) < deviationb
deviationb = std(c);
solutioni = i
solutionj = j
This code worked when I ran it, but I think you need to take another look at your problem.
Sean de Wolski on 3 Jun 2011
For the above example and any other permutation defined by it the best case scenario is all four weights to equal zero and the positions equal to whatever. That is, your 'b' vector is not completely full b(11) is still zero. As long 'b' can have non-a values, then there's no reason to have non-zero values since this WILL be the minimum standard deviation.
Assuming, you want to constrain all 'b' to not also be in 'a' it should be possible to set it up for fmincon, as an optimization problem of six variables: two positions and four weights, the positions constrained to integers in the middle so that the middle is covered. The boundaries are fixed since you would NEED one position to be 1, and the other to be length(b)-length(a)+1.
Just a few ideas hopefully someone can build on.