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:
Norm giving wrong answer!!

Subject: Norm giving wrong answer!!

From: Berk Birand

Date: 1 Oct, 2010 19:08:19

Message: 1 of 7

Hi everyone,

Forgive the exclamation points in the subject, but this is an incredible problem, and I can't believe it's happening. I am trying to compute the Euclident distance, L2 norm between two vectors. I simply wanted to use norm(a-b), but noticed that it simply doesn't give the right 2-norm, even when I specify it as the second parameter. After trying to figure out what was wrong, it's incredible how it won't give the right answer!

When I open up a new file, and put the following in it:
clear
x1=1490;
x2=1510;
y1= 1500;
y2=1500;
[x1-x2, y1-y2]
norm( [x1 -x2, y1-y2] )
sqrt(sum(([x1-x2, y1-y2]).^2))
which norm

if norm( [x1 -x2, y1-y2] ) ~= sqrt(sum(([x1-x2, y1-y2]).^2))
    error('Problem in the norm?!?');
end

Running this gives me:

ans =
   -20 0
ans =
   2.1214e+03
ans =
    20
built-in (/Applications/MATLAB_R2008b.app/toolbox/matlab/matfun/@single/norm) % single method
??? Problem in the norm?!?
 
Can you believe this!? This just can't be a bug, but I really don't think I'm doing something wrong.
Any ideas?! AM I CRAZY!?!? :)

Thanks,
 bB

Subject: Norm giving wrong answer!!

From: Sean

Date: 1 Oct, 2010 19:17:21

Message: 2 of 7

"Berk Birand" <graffitici_remove_this_@yahoo.com> wrote in message <i85bj3$am5$1@fred.mathworks.com>...
> Hi everyone,
>
> Forgive the exclamation points in the subject, but this is an incredible problem, and I can't believe it's happening. I am trying to compute the Euclident distance, L2 norm between two vectors. I simply wanted to use norm(a-b), but noticed that it simply doesn't give the right 2-norm, even when I specify it as the second parameter. After trying to figure out what was wrong, it's incredible how it won't give the right answer!
>
> When I open up a new file, and put the following in it:
> clear
> x1=1490;
> x2=1510;
> y1= 1500;
> y2=1500;
> [x1-x2, y1-y2]
> norm( [x1 -x2, y1-y2] )
> sqrt(sum(([x1-x2, y1-y2]).^2))
> which norm
>
> if norm( [x1 -x2, y1-y2] ) ~= sqrt(sum(([x1-x2, y1-y2]).^2))
> error('Problem in the norm?!?');
> end
>
> Running this gives me:
>
> ans =
> -20 0
> ans =
> 2.1214e+03
> ans =
> 20
> built-in (/Applications/MATLAB_R2008b.app/toolbox/matlab/matfun/@single/norm) % single method
> ??? Problem in the norm?!?
>
> Can you believe this!? This just can't be a bug, but I really don't think I'm doing something wrong.
> Any ideas?! AM I CRAZY!?!? :)

You may not be crazy but your syntax are wrong:
%%
x1=1490;
x2=1510;
y1= 1500;
y2=1500;
if norm( [[x1-x2],[y1-y2]] ) ~= sqrt(sum(([x1-x2, y1-y2]).^2))
    error('Problem in the norm?!?');
end
%%

Or there's always the possibility that you may be crazy ;)

Subject: Norm giving wrong answer!!

From: Matt J

Date: 1 Oct, 2010 19:22:06

Message: 3 of 7

"Berk Birand" <graffitici_remove_this_@yahoo.com> wrote in message <i85bj3$am5$1@fred.mathworks.com>...

> norm( [x1 -x2, y1-y2] )
=====

What you're seeing is not a problem with norm().
It's due to the sensitivity of MATLAB matrix expressions to stray spaces. If you get rid of the space between x1 and -x2 in the above expression, you'll see the answer you want

>> norm( [x1-x2, y1-y2] )

ans =

    20


Still confused? Then compare the following

>> [x1 -x2, y1-y2]

ans =

        1490 -1510 0

>> [x1-x2, y1-y2]

ans =

   -20 0

Subject: Norm giving wrong answer!!

From: Bruno Luong

Date: 1 Oct, 2010 19:25:23

Message: 4 of 7

"Berk Birand" <graffitici_remove_this_@yahoo.com> wrote in message <i85bj3$am5$1@fred.mathworks.com>...

> norm( [x1 -x2, y1-y2] )
                 ^ This space causes the problem, delete it.

Bruno

Subject: Norm giving wrong answer!!

From: Walter Roberson

Date: 1 Oct, 2010 19:26:55

Message: 5 of 7

On 10-10-01 02:08 PM, Berk Birand wrote:
> norm( [x1 -x2, y1-y2] )
> sqrt(sum(([x1-x2, y1-y2]).^2))

Spacing is important.
[x1 -x2, y1-y2] is [(x1),(-x2),(y1-y2)] which is a three-element vector.

Subject: Norm giving wrong answer!!

From: Jan Simon

Date: 1 Oct, 2010 20:17:21

Message: 6 of 7

Dear Berk,

After reading several style guides, I suggest to insert spaces around operators, instead of removing them:

> > [x1 -x2, y1-y2]
Either:
  [x1-x2, y1-y2]
or - nicer in my opinion and to several style guides for C, C++, M and hopefully Basic:
  [x1 - x2, y1 - y2]

Jan

Subject: Norm giving wrong answer!!

From: James Tursa

Date: 1 Oct, 2010 20:36:20

Message: 7 of 7

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i85fkh$9tn$1@fred.mathworks.com>...
> Dear Berk,
>
> After reading several style guides, I suggest to insert spaces around operators, instead of removing them:
>
> > > [x1 -x2, y1-y2]
> Either:
> [x1-x2, y1-y2]
> or - nicer in my opinion and to several style guides for C, C++, M and hopefully Basic:
> [x1 - x2, y1 - y2]
>
> Jan

Whenever there are element expressions involved in building a matrix this way, I always use parentheses. That way I never have to worry about spaces. e.g.,

   [(x1 - x2), (y1 - y2)]

You can insert or delete spaces anywhere you like in the above expression and always get the same result.

James Tursa

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