Quadratic Program formulation: Matlab - matlab

Given this objective function:
Minimize:
f = (Ax + By)' * G * (Ax + By)
subject to some equalities and inequalities.
where x and y are real-valued vectors (decision variables) with p and q elements, respectively. A of size m * p, B of size m * q, G is a symmetric matrix of size m * m.
My question is how to write f in the form v' * G * v, such that it can be easily be used in quadprog. In other words, how to mix A, B and G?

This looks incompletely specified!
It seems, for whatever reason, you want to model in terms of two variable components. Now you did not specify how they interact with each other.
As most optimizers work on a single variable-vector, you need to concatenate yours.
As you did not show G, i'm assuming you got one G for x and one for y, let's call it H.
(Remark: not a matlab user; don't take example-syntax for granted!)
z = [x y]
P = blkdiag(G,H)
assuming x and y independent in regards to quadratic-term
e.g. no x0*y1 like terms
Solve: for z` P z
Example:
x = [x0 x1 x2]
y = [y0 y1]
G = [6 2 1; 2 5 2; 1 2 4]
H = [8 2; 2 10]
# G
6 2 1
2 5 2
1 2 4
# H
8 2
2 8
z = [x0 x1 x2 y0 y1]
P = [6 2 1 0 0; 2 5 2 0 0; 1 2 4 0 0; 0 0 0 8 2; 0 0 0 2 8]
# P
6 2 1 0 0
2 5 2 0 0
1 2 4 0 0
0 0 0 8 2
0 0 0 2 8

Related

Create matrix with N-4 rows and N-2 columns

Would you give me any comments on this question? I need to create a matrix in Matlab, following the description below:
for a given N and three values a, b and c. I want to create a (N-4)x(N-2) matrix, e.g. for N=8
a b c 0 0 0
0 a b c 0 0
0 0 a b c 0
0 0 0 a b c
Thanks a Lot
What you are trying to create is a toeplitz matrix. One you know the name, it's simple.
%construct the first row
row=zeros(N-2,1);
row(1:3)=[a,b,c];
%construct the first column
col=zeros(N-4,1);
col(1)=a;
%call the function
toeplitz(col,row)
Result with a=4;b=2;c=3;N=8;
ans =
4 2 3 0 0 0
0 4 2 3 0 0
0 0 4 2 3 0
0 0 0 4 2 3
You could solve using the identity matrix function eye() and some matrix concatenation:
N = 8;
a = 1;
b = 2;
c = 3;
a_I = [zeros(N-4, 0), a*eye(N-4), zeros(N-4, 2)];
b_I = [zeros(N-4, 1), b*eye(N-4), zeros(N-4, 1)];
c_I = [zeros(N-4, 2), c*eye(N-4), zeros(N-4, 0)];
X = a_I + b_I + c_I
Which produces the output
X =
1 2 3 0 0 0
0 1 2 3 0 0
0 0 1 2 3 0
0 0 0 1 2 3
You can also make it slightly more general and let the scalars a, b and c be contained in a vector:
N = 8;
v = [1 2 3];
X = zeros(N-4, N-2);
for ii = 1:length(v)
X = X + [zeros(N-4, ii-1), v(ii)*eye(N-4), zeros(N-4, length(v)-ii)];
end
Which produces the same output.
try this fully vectorized function:
function b=bandmat(a, N, M)
% license: CC-BY
% example: bandmat([1,2,3],4,6)
b=a(:).';
b(M+1)=0;
b=repmat(b,1,N);
b=reshape(b(1:end-N),M,N).';

Matlab compare two matrixes with different dimension

I see people take ==, ~=, >, < between matrixes with a different dimension in parentheses following a matrix to get its entries, like this:
b =
1 4 7
2 5 8
3 6 9
>> b == [1 2 3]
ans =
3×3 logical array
1 0 0
0 0 0
0 0 0
>> b == [1 4 7]
ans =
3×3 logical array
1 1 1
0 0 0
0 0 0
>> b == [1 4 5]
ans =
3×3 logical array
1 1 0
0 0 0
0 0 0
>> b == [1 5 4]
ans =
3×3 logical array
1 0 0
0 1 0
0 0 0
>> a
a =
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
>> a(:, b == [1 4 5])
ans =
1 4
1 4
1 4
>> a(:, b == [1 5 4])
ans =
1 5
1 5
1 5
>> b
b =
1 4 7
2 5 8
3 6 9
>> b > [1 3 2]
ans =
3×3 logical array
0 1 1
1 1 1
1 1 1
However, I have no idea why these would work. Any explanation about this usage? (My English is not good to describe the question better, hope anyone could edit this question to make it more understandable? Thanks in advance!)
Since R2016b, MATLAB uses implicit expansion. This means that inputs' singleton dimensions (dimensions of size 1) are repeated to be the same size as other inputs.
For example:
b = [1 4 7
2 5 8
3 6 9]
%% Singleton dimension as rows
b == [x y z]
% is equivalent to
b == repmat([x y z], size(b,1), 1)
% is equivalent to
b == [x y z
x y z
x y z]
%% Singleton dimension as columns
b == [x; y; z]
% is equivalent to
b == repmat([x; y; z], 1, size(b,2))
% is equivalent to
b == [x x x
y y y
z z z]
All 3 of your examples are the "singleton dimension as rows" case, repeating your comparison row vector down and comparing to each row of b. You can easily see that the outputs are as expected.
Implicit expansion can be used in all versions of MATLAB (including pre-R2016b) with the use of bsxfun. This would look like so:
% Since 2016b
b == [1 2 3];
% All versions (#eq is the '==' equals function)
bsxfun(#eq, b, [1 2 3])

Creating Matrix with a loop in Matlab

I want to create a matrix of the following form
Y = [1 x x.^2 x.^3 x.^4 x.^5 ... x.^100]
Let x be a column vector.
or even some more variants such as
Y = [1 x1 x2 x3 (x1).^2 (x2).^2 (x3).^2 (x1.x2) (x2.x3) (x3.x1)]
Let x1,x2 and x3 be column vectors
Let us consider the first one. I tried using something like
Y = [1 : x : x.^100]
But this also didn't work because it means take Y = [1 x 2.*x 3.*x ... x.^100] ?
(ie all values between 1 to x.^100 with difference x)
So, this also cannot be used to generate such a matrix.
Please consider x = [1; 2; 3; 4];
and suggest a way to generate this matrix
Y = [1 1 1 1 1;
1 2 4 8 16;
1 3 9 27 81;
1 4 16 64 256];
without manually having to write
Y = [ones(size(x,1)) x x.^2 x.^3 x.^4]
Use this bsxfun technique -
N = 5; %// Number of columns needed in output
x = [1; 2; 3; 4]; %// or [1:4]'
Y = bsxfun(#power,x,[0:N-1])
Output -
Y =
1 1 1 1 1
1 2 4 8 16
1 3 9 27 81
1 4 16 64 256
If you have x = [1 2; 3 4; 5 6] and you want Y = [1 1 1 2 4; 1 3 9 4 16; 1 5 25 6 36] i.e. Y = [ 1 x1 x1.^2 x2 x2.^2 ] for column vectors x1, x2 ..., you can use this one-liner -
[ones(size(x,1),1) reshape(bsxfun(#power,permute(x,[1 3 2]),1:2),size(x,1),[])]
Using an adapted Version of the code found in Matlabs vander()-Function (which is also to be found in the polyfit-function) one can get a significant speedup compared to Divakars nice and short solution if you use something like this:
N = 5;
x = [1:4]';
V(:,n+1) = ones(length(x),1);
for j = n:-1:1
V(:,j) = x.*V(:,j+1);
end
V = V(:,end:-1:1);
It is about twice as fast for the example given and it gets about 20 times as fast if i set N=50 and x = [1:40]'. Although I state that is not easy to compare the times, just as an option if speed is an issue, you might have a look at this solution.
in octave, broadcasting allows to write
N=5;
x = [1; 2; 3; 4];
y = x.^(0:N-1)
output -
y =
1 1 1 1 1
1 2 4 8 16
1 3 9 27 81
1 4 16 64 256

From matrix column subtract corresponding vector value

I have a matrix 'x' and a row vector 'v'; the number of elements in the row vector is the same as the number of columns in the matrix. Is there any predefined function for doing the following operation?
for c = 1 : columns(x)
for r = 1 : rows(x)
x(r, c) -= v(c);
end
end
bsxfun(#minus,x,v)
Here's an octave demonstration:
octave> x = [1 2 3;2 3 4]
x =
1 2 3
2 3 4
octave> v = [2 0 1]
v =
2 0 1
octave>
octave> z=bsxfun(#minus,x,v)
z =
-1 2 2
0 3 3
If you are using Octave 3.6.0 or later, you don't have to use bsxfun since Octave performs automatic broadcasting (note that this is the same as actually using bsxfun, just easier on the eye). For example:
octave> x = [1 2 3; 2 3 4]
x =
1 2 3
2 3 4
octave> v = [2 0 1]
v =
2 0 1
octave> z = x - v
z =
-1 2 2
0 3 3
Alternatively, you can replicate your vector and directly subtract it from the matrix
z = x-repmat(v, size(x, 1), 1);

Set column to 0 with probability p

I've got a matrix A with the dimensions m X n. For every column i (i > 0and i <= n) I want to flip a coin and fill the whole column with 0 values with probability p. How can this be accomplished in MATLAB?
Example:
A = [1 2 3 4; 5 6 7 8] and p = 0.5 could result in
A' = [1 0 3 0; 5 0 7 0]
You can use the function rand() to generate an array of uniformly distributed random numbers, and use logical indexing to select colums where that array is less than p:
A = [1 2 3 4; 5 6 7 8];
p = 0.5;
A(:, rand(size(A,2), 1)<p) = 0
A =
0 2 0 0
0 6 0 0
You can do something like bsxfun(#times, A, rand(1, size(A, 2)) > p). Alex's answer is admittedly better, though.