I want to new vector with zero mean

조회 수: 4 (최근 30일)
Hyder Ali 2019년 6월 4일
답변: Image Analyst 2019년 6월 6일
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;

댓글을 달려면 로그인하십시오.

답변 (2개)

Priysha Aggarwal 2019년 6월 4일
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이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Image Analyst 2019년 6월 6일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by