I want to new vector with zero mean
4 views (last 30 days)
Show older comments
I want to have mean of a vector zero.
for example I have a vector y=[12 -10 15 23];
I want a new vector (X) which has zero mean but each element of X should be less than each element of y.
This means x1 < 12, x2<-10, x3<13 and x4<23 ---------- with conditionation that mean(x1,x2,x3,x4) = 0;
0 Comments
Answers (2)
Priysha Aggarwal
on 4 Jun 2019
You can try x = y-mean(y).
However, the above solution wont work if you have a negative mean(y).
x=y-mean(y) will increase the value when mean is negative.
Solution :
if (mean(y) > 0)
x = y - mean(y)
else
x = y + mean(y)
0 Comments
Image Analyst
on 6 Jun 2019
Hyder:
Here is a general solution that should work for any y:
y=[12 -10 15 23]
% Find out which indexes represent positive y.
indexesAbove = y > 0;
% Find out which indexes represent negative y.
indexesBelow = y < 0;
yAbove = y(indexesAbove)
yBelow = y(indexesBelow)
% Get sums above and below zero.
sumOfYAbove = sum(yAbove)
sumOfYBelow = sum(yBelow)
% Multiply yAbove by factor a to make them less than the positive y.
% Subtract b from yBelow to make it less than negative y.
% To get a mean of zero, you must have
% a * sumOfYAbove + (sumOfYBelow - b) = 0
% a * sumOfYAbove = b - sumOfYBelow
% So arbitrarily pick any a, say, a = 0.5 or whatever you want.
% Then determine b:
% b = a * sumOfYAbove + sumOfYBelow
a = 0.5
b = a * sumOfYAbove + sumOfYBelow
% Now construct new vectors
yNew = y; % Initialize.
yNew(indexesAbove) = a * yNew(indexesAbove);
yNew(indexesBelow) = yNew(indexesBelow) - b;
% Show new yMean in command window.
yNew
% Show (prove) that the new mean is zero:
newMean = mean(yNew)
It shows:
a =
0.5
b =
15
yNew =
6 -25 7.5 11.5
newMean =
0
0 Comments
See Also
Categories
Find more on Logical 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!