Given a set of n vectors a_i, I am trying to find a vector x to minimize the expression |a_1-x|^p+|a_2-x|^p+|a_3-x|^p+…+|a_n-x|^p, where p is greater than 1.
I am having trouble with writing this sum expression in cvx syntax. Could someone explain what is wrong with the code below? In the code I try to initialize 5 15x1 vectors in sample, and try to find the correct value of x.
sample - x is not dimensionally consistent; that is a (15 by 5) matrix minus a (5 by 1) vector. Also, |a_1-x|^p doesn’t even make sense when x is a vector, not a scalar. Perhaps you want p norm to the pth power of a_1 - x ?
If p = 1 or 2, you can use a very compact and vectorized implementation:
m = 15; n= 5;
samplee = rand(m,n);
p = 2;
Here is a more brute force way which will work for any p >= 1.
p = 2; % p = 1.5 also works, even p = inf
Obj = 0;
for i = 1:n
Obj = Obj + norm(samplee(:,i)-x,p);;
For p =1 or 2, both programs work, and for the same instantiation of samplee, produce the same results, within numerical tolerance
For any p >= 1 (but not inf), you could instead make your objective be the sum of norm^p by changing Obj = Obj + norm(samplee(:,i)-x,p);
in the 2nd version to Obj = Obj + pow_pos(norm(samplee(:,i)-x,p),p);