From: Frédéric Bergeron on
Hi all,

First, thanks for the answer I got in the past in this forum, hope I'll get some today! :)

My situation:
I got a set of 1-dimensional data (data) and I want to find the scalar x that respect a previously defined ratio R:

R=sum(data(over)-x)/sum(x-data(under));
where:
data(over) represent all the data over x: data(over)=(data>=x);
data(under) represent all the data under x: data(under)=(data<x);

So R is the ratio of the sum of difference between data over x and x, over the sum of difference between data under x and x.

My problem is that I need the value of x to define data(over) and data(under). Is there's a function that could help me? I read about lsqlin() that can solve a problem with inequality constraint but I didn't get success at using it.

Thanks in advance!
Best regards,
From: Alan Weiss on
On 6/28/2010 9:36 AM, Frédéric Bergeron wrote:
> Hi all,
>
> First, thanks for the answer I got in the past in this forum, hope I'll
> get some today! :)
>
> My situation:
> I got a set of 1-dimensional data (data) and I want to find the scalar x
> that respect a previously defined ratio R:
>
> R=sum(data(over)-x)/sum(x-data(under));
> where:
> data(over) represent all the data over x: data(over)=(data>=x);
> data(under) represent all the data under x: data(under)=(data<x);
>
> So R is the ratio of the sum of difference between data over x and x,
> over the sum of difference between data under x and x.
>
> My problem is that I need the value of x to define data(over) and
> data(under). Is there's a function that could help me? I read about
> lsqlin() that can solve a problem with inequality constraint but I
> didn't get success at using it.
>
> Thanks in advance!
> Best regards,

I think you would do better to use fzero.

Alan Weiss
MATLAB mathematical toolbox documentation
From: Matt J on
"Frédéric Bergeron" <frederic.bergeron(a)logiag.com> wrote in message <i0a8gm$kju$1(a)fred.mathworks.com>...

> My problem is that I need the value of x to define data(over) and data(under). Is there's a function that could help me? I read about lsqlin() that can solve a problem with inequality constraint but I didn't get success at using it.
=======

I could be wrong, but it seems like you can solve this by simple algebra. Let's make the following definitions

N=length(data); %known
S=sum(data); %known
O=sum(data(over)-x)
U=sum(x-data(under))

Then you have 2 equations that allow you to solve for O and U:

O-R*U=0
O+U=S;

Once you know O and U, you have the relationship

O-U=S-N*x

which can be used to solve explicitly for x

x=(S+U-O)/N
From: Matt J on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i0ad6n$91i$1(a)fred.mathworks.com>...

>
> I could be wrong, but it seems like you can solve this by simple algebra. Let's make the following definitions
>
> N=length(data); %known
> S=sum(data); %known
> O=sum(data(over)-x)
> U=sum(x-data(under))
>
> Then you have 2 equations that allow you to solve for O and U:
>
> O-R*U=0
> O+U=S;
========

Forget it. I pulled the equation O+U=S from my imagination somehow.

In any case, there are at most N different possibilities for the sets
data(over) and data(under). For each one, x ranges over a known interval separating data(over) from data(under) and in that interval O -R*U is a known linear function of x. You could simply loop over each of these N sets and test whether
O(x)-R*U(x) has a root in that interval.


From: Matt J on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i0afpa$3is$1(a)fred.mathworks.com>...

> In any case, there are at most N different possibilities for the sets
> data(over) and data(under). For each one, x ranges over a known interval separating data(over) from data(under) and in that interval O -R*U is a known linear function of x. You could simply loop over each of these N sets and test whether
> O(x)-R*U(x) has a root in that interval.
===========

The following code implements this, assuming that all data points are unique. I leave the generalization as an exercise.

%data
R=0.75;
data=rand(1000,1);


%engine
data=sort(data);
S=sum(data);
N=length(data);

Sunder=cumsum(data(1:end-1));
Sover=S-Sunder;
Nunder=(1:N-1).';
Nover=N-Nunder;


X=(Sover+R*Sunder)./(Nover+R*Nunder);

x=X(X>=data(1:end-1) & X<=data(2:end))

%Check
over=(data>=x);
under=~over;
Rcheck=sum(data(over)-x)/sum(x-data(under))