Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
double moon classification problem

Subject: double moon classification problem

From: Umit Yilmaz

Date: 17 Oct, 2010 16:07:03

Message: 1 of 3

Hi friends,

I want to reproduce this matlab code with using the method of least squares estimation:

Can you help me?

------------------------------------------------------------------------------------------------------------------------

function data=dm(r,w,ts,d)
clear all; close all;
if nargin<4, w=6;end
if nargin<3, r=10;end
if nargin<2, d=1;end
if nargin < 1, ts=1000; end
ts1=10*ts;
done=0; tmp1=[];
while ~done,
    tmp=[2*(r+w/2)*(rand(ts1,1)-0.5) (r+w/2)*rand(ts1,1)];
    tmp(:,3)=sqrt(tmp(:,1).*tmp(:,1)+tmp(:,2).*tmp(:,2));
    idx=find([tmp(:,3)>r-w/2] & [tmp(:,3)<r+w/2]);
    tmp1=[tmp1;tmp(idx,1:2)];
    if length(idx)>= ts,
        done=1;
    end
end
data=[tmp1(1:ts,:) zeros(ts,1);
    [tmp1(1:ts,1)+r -tmp1(1:ts,2)-d ones(ts,1)]];
plot(data(1:ts,1),data(1:ts,2),'.r',data(ts+1:end,1),data(ts+1:end,2),'.b');
title(['Perceptron with the double-moon set at distance d = ' num2str(d)]),
axis([-r-w/2 2*r+w/2 -r-w/2-d r+w/2])
save dm r w ts d data;

------------------------------------------------------------------------------------------------------------------------

Subject: double moon classification problem

From: Greg Heath

Date: 17 Oct, 2010 18:57:41

Message: 2 of 3

On Oct 17, 12:07 pm, "Umit Yilmaz" <umityil...@hotmail.com> wrote:
> Hi friends,
>
> I want to reproduce this matlab code with using the method of least squares estimation:
>
> Can you help me?
>
> ---------------------------------------------------------------------------­---------------------------------------------
>
> function data=dm(r,w,ts,d)
> clear all; close all;
> if nargin<4, w=6;end
> if nargin<3, r=10;end
> if nargin<2, d=1;end
> if nargin < 1, ts=1000; end

This make more sense:

if nargin<4, d = defaultd; end
if nargin<3, ts = defaultts; end
if nargin<2, w = defaultw; end
if nargin<1, r = defaultr; end

Hope this helps.

Greg

Subject: double moon classification problem

From: Roger Stafford

Date: 17 Oct, 2010 20:51:03

Message: 3 of 3

"Umit Yilmaz" <umityilmax@hotmail.com> wrote in message <i9f6v7$3ob$1@fred.mathworks.com>...
> I want to reproduce this matlab code with using the method of least squares estimation:
> .......
> while ~done,
> tmp=[2*(r+w/2)*(rand(ts1,1)-0.5) (r+w/2)*rand(ts1,1)];
> tmp(:,3)=sqrt(tmp(:,1).*tmp(:,1)+tmp(:,2).*tmp(:,2));
> idx=find([tmp(:,3)>r-w/2] & [tmp(:,3)<r+w/2]);
> tmp1=[tmp1;tmp(idx,1:2)];
> if length(idx)>= ts,
> done=1;
> end
> end
> .......
- - - - - - - - -
  The code you show is inefficient for at least two reasons. First, in your 'while' loop you are waiting until at least ts out of 10*ts satisfy your test during any single pass, even though the total number that have satisfied it from previous passes may have already exceeded ts. Why not simply turn on 'done' when the total number in size(tmp1,1) exceeds ts?

  The second inefficiency is your manner of generating random points in the half annulus which are statistically uniform within the annulus' area. If you do things right, you can avoid rejecting any of your random points and still have an area-wise uniform distribution. No 'while' loop would be required.

  Just do the following to cure both shortcomings:

theta = pi*rand(ts,1); % Range through half circle by angle theta
rad = sqrt(2*r*w*rand(ts,1)+(r-w/2)^2); % r-w/2 < rad < r+w/2
tmp1 = [rad.*cos(theta),rad.*sin(theta)];

Because of the square root in 'rad', these points will all be statistically uniform area-wise throughout the interior of the half annulus and none will fall outside it.

  However, I don't know what any of this has to do with "least squares estimation".

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us