Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

linspace(2,2,2) returns [2 2] ??

263 views
Skip to first unread message

Nasser M. Abbasi

unread,
Oct 10, 2010, 2:01:29 PM10/10/10
to
May be the help for linspace should change?

It says:

"LINSPACE(X1, X2, N) generates N points between X1 and X2."


Notice, it says BETWEEN.

But when I ask for 2 points, between 2 and 2:

----------------
EDU>> linspace(2,2,2)

ans =

2 2
---------------

May be mathematically this is ok? i.e. there are infinite points between
the point and itself? (on the real line), but from programming point of
view, it is confusing. no?

--Nasser

Image Analyst

unread,
Oct 10, 2010, 2:16:04 PM10/10/10
to
Maybe it should say "generates N points between X1 and X2, inclusive."

What did you expect to get back as an answer from issuing linspace(2,2,2)?

I didn't understand your statement "there are infinite points between the point and itself?" How do you have an infinite number of points between 2 and 2? I can see an infinite number of points between 2 and something that's not itself, like 2.00000000000000001, but if they are absolutely perfectly identical how are you getting an infinite number of points?

Nasser M. Abbasi

unread,
Oct 10, 2010, 2:37:26 PM10/10/10
to
On 10/10/2010 11:16 AM, Image Analyst wrote:
> Maybe it should say "generates N points between X1 and X2, inclusive."
>
> What did you expect to get back as an answer from issuing linspace(2,2,2)?
>

I was just not expecting it to "work". may be an error message. That is
not the point. The point is that it did work.

> I didn't understand your statement "there are infinite points between the point and itself?"
> How do you have an infinite number of points between 2 and 2?

But that is my point. How come? Matlab says so. Ask matlab for one
million points between 2 2 and it will return one million points between
2 and 2.

I am was trying to possibly given some hand waving pure mathematical
justification of why Matlab did that. The idea is that some pure math
person could argue that there are infinite points on the real point. No
matter how small the delta gets, there are still infinite more point.
But since we are on a real discrete machine, this should not apply.

I was not supporting what linespace does, merely trying to think
out-loud what was in the mind of who wrote it to work this way.

>I can see an infinite number of points between 2 and something that's not itself.

Exactly. That is why I posted this. So you agree with me that there is
"something" wrong here. May be we agree that the documentation needs to
be more clear to justify this result.

--Nasser

Bruno Luong

unread,
Oct 10, 2010, 2:53:04 PM10/10/10
to
"Nasser M. Abbasi" <n...@12000.org> wrote in message <i8sv1m$5k3$1...@speranza.aioe.org>...

> May be the help for linspace should change?
>
> It says:
>
> "LINSPACE(X1, X2, N) generates N points between X1 and X2."

I understand they are "linearly equally spaced" from the previous sentence of the help. i.e., it will returns (for N>=2):

y(k) = X1+(k-1)*(X2-X1)/(N-1), for k=1,...N

If I plug X1 = 2, X2 = 2, and N = 2 It will returns y(1)=2 and y(2) =2. It looks perfectly consistent to me.

The help says it returns "N points in between" (of the closed-inverval), it does not claims there are at least "N *distinct* points in between". So I don't see any problem mathematically either.

Bruno

Nasser M. Abbasi

unread,
Oct 10, 2010, 3:08:30 PM10/10/10
to
On 10/10/2010 11:53 AM, Bruno Luong wrote:

>
> The help says it returns "N points in between" (of the closed-inverval),
> it does not claims there are at least "N *distinct* points in between".

For me, "in between" implies distinct points.

"between" implies a delta, hence different points, hence distinct.

You said yourself, there can't be points between a point and itself.

Hence, the points returned must be distinct points.

>
> Bruno


--Nasser

Bruno Luong

unread,
Oct 10, 2010, 3:14:04 PM10/10/10
to
"Nasser M. Abbasi" <n...@12000.org> wrote in message <i8t2vb$gkl$1...@speranza.aioe.org>...

> On 10/10/2010 11:53 AM, Bruno Luong wrote:
>
> >
> > The help says it returns "N points in between" (of the closed-inverval),
> > it does not claims there are at least "N *distinct* points in between".
>
> For me, "in between" implies distinct points.
>
> "between" implies a delta, hence different points, hence distinct.
>


Mathematically, a point X is in between A and B if

A<=X<=B

> You said yourself, there can't be points between a point and itself.

Did I said that?

Bruno

Image Analyst

unread,
Oct 10, 2010, 3:40:05 PM10/10/10
to
"Nasser M. Abbasi" <n...@12000.org> wrote in message <i8t153$asm$1...@speranza.aioe.org>...

> On 10/10/2010 11:16 AM, Image Analyst wrote:
> > What did you expect to get back as an answer from issuing linspace(2,2,2)?
> >
>
> I was just not expecting it to "work". may be an error message. That is
> not the point. The point is that it did work.
----------------------------------------------------------

If you were the programmer to program up linspace, what you *you* do? Sounds like you would throw an exception if someone passed in X1 = X2. I'm not sure I or others would like that, especially since you can always check for this situation before calling linespace:
if abs(X1-X2) < eps
warndlg()
else
linspace()
end
It doesn't seem right to generate an error for those who have no problem with the way it operates. You always have the choice to check, but why remove the choice to use it that way for them?

Nasser M. Abbasi

unread,
Oct 10, 2010, 3:59:52 PM10/10/10
to
On 10/10/2010 12:14 PM, Bruno Luong wrote:

>
>> You said yourself, there can't be points between a point and itself.
>


> Did I said that?
>
> Bruno

Sorry, mixed you up with ImageAnalyst, who said:

" I can see an infinite number of points between 2 and something that's
not itself"


Was not looking too carfully on who I was answering.

--Nasser

Nasser M. Abbasi

unread,
Oct 10, 2010, 4:09:26 PM10/10/10
to
On 10/10/2010 12:40 PM, Image Analyst wrote:
> "Nasser M. Abbasi"<n...@12000.org> wrote in message<i8t153$asm$1...@speranza.aioe.org>...
>> On 10/10/2010 11:16 AM, Image Analyst wrote:
>>> What did you expect to get back as an answer from issuing linspace(2,2,2)?
>>>
>>
>> I was just not expecting it to "work". may be an error message. That is
>> not the point. The point is that it did work.
> ----------------------------------------------------------
>

> If you were the programmer to program up linspace, what you *you* do?

But that is NOT the point.

You are jumping on an implementation issue.

First, we decide if the "behavior" is correct based on the
specification/documentation.

Next, if we agree that the "behavior" is inconsistent with the the
specification/documentation, then the next point is what to do.

We can measure the cost of making it behave as documented in this case,
vs. leaving things the way they are and may be improve the documentation.

You already jumped on the cost of changing the implementation, before we
agreed that the "behavior" is not consistent with the specifications.

For me, what to do, is a separate design issue, I am merely pointing
what I think is an inconsistent behavior of a function. That is all.

>Sounds like you would throw an exception if someone passed in X1 = X2. I'm not sure I or others would
>like that, especially since you can always check for this situation
before calling linespace:
> if abs(X1-X2)< eps
> warndlg()
> else
> linspace()
> end
> It doesn't seem right to generate an error for those who have no problem with the way it operates.
> You always have the choice to check, but why remove the choice to use
it that way for them?

You already suggested what I would, and then starting to argue that what
I would not is not right? You'd make a good lawyer :)

--Nasser

Steven_Lord

unread,
Oct 10, 2010, 11:45:33 PM10/10/10
to

"Nasser M. Abbasi" <n...@12000.org> wrote in message

news:i8sv1m$5k3$1...@speranza.aioe.org...

linspace(x1, x2, N) returns a 1-by-max(0, N) vector containing N equally
spaced points. If N > 0, the first element of the vector is x1; if N > 1
then the last element of the vector is x2. If x1 == x2, then the equal
spacing between points is 0 and the vector will consist of N copies of x1.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Bruno Luong

unread,
Oct 11, 2010, 2:08:04 AM10/11/10
to
"Nasser M. Abbasi" <n...@12000.org> wrote in message <i8t2vb$gkl$1...@speranza.aioe.org>...

>
>
> "between" implies a delta, hence different points, hence distinct.
>

It seems like you describe "colon" operator. No where LINSPACE documentation states it must ensure a strictly positive step, thus distinct output.

In fact the second input can be larger or smaller or EQUAL than the first input. The output are linear spaced between then regardless which one is larger.

The doc is perfectly consistent.

Bruno

Bruno Luong

unread,
Oct 11, 2010, 2:17:04 AM10/11/10
to
This might be more subtle and worth to explain to students:

>> a=linspace(1,1+eps);
>> length(a)

ans =

100

>> length(unique(a))

ans =

2
>> sum(a==1)

ans =

50

>> sum(a==1+eps)

ans =

50

>>

% Bruno

Francis Burton

unread,
Oct 11, 2010, 6:24:44 AM10/11/10
to
In article <i8u18u$9oh$1...@fred.mathworks.com>,

Steven_Lord <sl...@mathworks.com> wrote:
>linspace(x1, x2, N) returns a 1-by-max(0, N) vector containing N equally
>spaced points. If N > 0, the first element of the vector is x1; if N > 1
>then the last element of the vector is x2. If x1 == x2, then the equal
>spacing between points is 0 and the vector will consist of N copies of x1.

I don't see how it could be more logical (or simple) than this.

Francis

Greg Heath

unread,
Oct 12, 2010, 9:32:27 PM10/12/10
to
On Oct 11, 6:24 am, fbur...@nyx.net (Francis Burton) wrote:
> In article <i8u18u$9o...@fred.mathworks.com>,

It's more complicates than that

>> for i = -2:3, linspace(2,2,i),end

ans = 2
ans = 2
ans = 2
ans = 2
ans = 2 2
ans = 2 2 2


>> for i = -2:3, linspace(2,1,i),end

ans = 1
ans = 1
ans = 1
ans = 1
ans = 2 1
ans = 2 1.5 1

Hope this helps.

Greg

Francis Burton

unread,
Oct 13, 2010, 8:28:56 AM10/13/10
to
In article <aae9a4af-8b70-4b0a...@u10g2000yqk.googlegroups.com>,
Greg Heath <he...@alumni.brown.edu> wrote:

>On Oct 11, 6:24?am, fbur...@nyx.net (Francis Burton) wrote:
>> In article <i8u18u$9o...@fred.mathworks.com>,
>>
>> Steven_Lord <sl...@mathworks.com> wrote:
>> >linspace(x1, x2, N) returns a 1-by-max(0, N) vector containing N equally
>> >spaced points. ?If N > 0, the first element of the vector is x1; if N > 1
>> >then the last element of the vector is x2. ?If x1 == x2, then the equal

>> >spacing between points is 0 and the vector will consist of N copies of x1.
>>
>> I don't see how it could be more logical (or simple) than this.
>
>It's more complicates than that
>
>>> for i = -2:3, linspace(2,2,i),end
>
>ans = 2
>ans = 2
>ans = 2
>ans = 2
>ans = 2 2
>ans = 2 2 2
>
>
>>> for i = -2:3, linspace(2,1,i),end
>
>ans = 1
>ans = 1
>ans = 1
>ans = 1
>ans = 2 1
>ans = 2 1.5 1
>
>Hope this helps.

So would you change the documentation or the implementation,
and how?

Francis

dpb

unread,
Oct 13, 2010, 8:48:03 AM10/13/10
to
...

IMO, no, not really.

Although, perhaps it could say "on the range of x1 to x2" instead and be
more precise.

--


Jan Simon

unread,
Oct 13, 2010, 9:33:11 AM10/13/10
to
Dear Nasser,

> "LINSPACE(X1, X2, N) generates N points between X1 and X2."

What about this:
"LINSPACE(X1, X2, N) generates N equally spaced points from X1 to X2."
(as ever, I'm not sure if this is correct English)

Anyhow, this mathematically vague help text does not seem to be a problem for a lot of Matlab users. In opposite to that the functional form of SAVE ("save('FileName', 'Variable')") is described exactly and correctly in the help section, but you can find a pile of posts conerning this in CSSM.

Jan

Greg Heath

unread,
Oct 13, 2010, 10:56:08 AM10/13/10
to
On Oct 13, 8:28 am, fbur...@nyx.net (Francis Burton) wrote:
> In article <aae9a4af-8b70-4b0a-8af5-e438961d8...@u10g2000yqk.googlegroups.com>,

Current version

>> help linspace

LINSPACE Linearly spaced vector.
LINSPACE(X1, X2) generates a row vector of 100 linearly
equally spaced points between X1 and X2.

LINSPACE(X1, X2, N) generates N points between X1 and X2.

For N < 2, LINSPACE returns X2.

See also LOGSPACE, :.

Modification

LINSPACE Vector with equally spaced elements.

LINSPACE(X1, X2) generates a row vector of 100 equally
spaced points between X1 and X2, inclusive.

LINSPACE(X1, X2, N) returns X2 for N < 2.

For N >=2, LINSPACE(X1, X2, N) generates a row vector
of floor(N) equally spaced points between X1 and
X2, inclusive.

See also LOGSPACE, :.

Hope this helps.

Greg

0 new messages