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:
numbers

Subject: numbers

From: Susan Montag

Date: 12 Mar, 2010 12:08:05

Message: 1 of 17

if I have a function called "numbers.m":

function list = numbers (a, b)


How can I let MatLab give all numbers between a and b?
So when I type "list = numbers (2, 5)", I want MatLab to give:

2
3
4
5

Anyone knows how to get this?

Subject: numbers

From: ImageAnalyst

Date: 12 Mar, 2010 12:17:48

Message: 2 of 17

Use the linspace() function.

Subject: numbers

From: dpb

Date: 12 Mar, 2010 14:09:34

Message: 3 of 17

ImageAnalyst wrote:
> Use the linspace() function.

or, if linsapce()'s behavior isn't precisely what want w/o an extra
parameter,

doc colon

should point the way...

--

Subject: numbers

From: Susan Montag

Date: 12 Mar, 2010 14:29:05

Message: 4 of 17

Ok thanks.

Now I'm getting:

list = 2 3 4 5

You know how to get

list = 2
        3
        4
        5

instead?

Subject: numbers

From: Matt Fig

Date: 12 Mar, 2010 14:39:05

Message: 5 of 17

use TRANSPOSE, or .' to transpose a vector.

Also, it seems like you could benefit from reading the 'Getting Started' section of the documentation.

Subject: numbers

From: Susan Montag

Date: 12 Mar, 2010 15:19:21

Message: 6 of 17

"Matt Fig" <spamanon@yahoo.com> wrote in message <hndjm9$pf3$1@fred.mathworks.com>...
> use TRANSPOSE, or .' to transpose a vector.
>
> Also, it seems like you could benefit from reading the 'Getting Started' section of the documentation.

Ok, well I already knew that but I was checking if it was right because I can't solve my main problem.

The real function is:
function list = numbers (a, b, c, d)

I have to calculate the numbers between a and b AND between c and d.
So when I type "list = numbers(2, 5, 1, 4)" a list like this should appear:

2 1
3 2
4 3
5 4


This is a little bit more complicated and I can't figure out how to code this.

Can anyone help me with this?

Subject: numbers

From: dpb

Date: 12 Mar, 2010 15:22:40

Message: 7 of 17

Susan Montag wrote:
> Ok thanks.
>
> Now I'm getting:
>
> list = 2 3 4 5
>
> You know how to get
>
> list = 2
> 3
> 4
> 5
>
> instead?

help punct
help transpose

Read the "Getting Started" section...

--

Subject: numbers

From: Susan Montag

Date: 12 Mar, 2010 15:49:22

Message: 8 of 17

"Matt Fig" <spamanon@yahoo.com> wrote in message <hndjm9$pf3$1@fred.mathworks.com>...
> use TRANSPOSE, or .' to transpose a vector.
>
> Also, it seems like you could benefit from reading the 'Getting Started' section of the documentation.

Ok, well I already knew that but I was checking if it was right because I can't solve my main problem.

The real function is:
function list = numbers (a, b, c, d)

I have to calculate the numbers between a and b AND between c and d.
So when I type "list = numbers(2, 5, 1, 4)" a list like this should appear:

2 1
3 2
4 3
5 4


This is a little bit more complicated and I can't figure out how to code this.

Can anyone help me with this?

Subject: numbers

From: Susan Montag

Date: 12 Mar, 2010 15:51:22

Message: 9 of 17

dpb <none@non.net> wrote in message <hndmag$7h5$2@news.eternal-september.org>...
> Susan Montag wrote:
> > Ok thanks.
> >
> > Now I'm getting:
> >
> > list = 2 3 4 5
> >
> > You know how to get
> >
> > list = 2
> > 3
> > 4
> > 5
> >
> > instead?
>
> help punct
> help transpose
>
> Read the "Getting Started" section...
>
> --


I already used transpose, but I can't get get it fixed:

The real function is:
function list = numbers (a, b, c, d)

I have to calculate the numbers between a and b AND between c and d.
So when I type "list = numbers(2, 5, 1, 4)" a list like this should appear:

2 1
3 2
4 3
5 4

So the problem is that I can't get it like this.

Subject: numbers

From: Susan Montag

Date: 12 Mar, 2010 16:01:22

Message: 10 of 17

"Susan Montag" <Sm@werew.com> wrote in message <hndntq$84c$1@fred.mathworks.com>...
> dpb <none@non.net> wrote in message <hndmag$7h5$2@news.eternal-september.org>...
> > Susan Montag wrote:
> > > Ok thanks.
> > >
> > > Now I'm getting:
> > >
> > > list = 2 3 4 5
> > >
> > > You know how to get
> > >
> > > list = 2
> > > 3
> > > 4
> > > 5
> > >
> > > instead?
> >
> > help punct
> > help transpose
> >
> > Read the "Getting Started" section...
> >
> > --
>
>
> I already used transpose, but I can't get get it fixed:
>
> The real function is:
> function list = numbers (a, b, c, d)
>
> I have to calculate the numbers between a and b AND between c and d.
> So when I type "list = numbers(2, 5, 1, 4)" a list like this should appear:
>
> 2 1
> 3 2
> 4 3
> 5 4
>
> So the problem is that I can't get it like this.

list = linspace (i1, i2, i2-i1+1)';

gives the result:

2
3
4
5

While I need

2 1
3 2
4 3
5 4

Subject: numbers

From: Matt Fig

Date: 12 Mar, 2010 16:01:23

Message: 11 of 17

[(2:5).' (1:4).']

Subject: numbers

From: Roger Stafford

Date: 12 Mar, 2010 16:26:20

Message: 12 of 17

"Susan Montag" <Sm@werew.com> wrote in message <hndm1p$448$1@fred.mathworks.com>...
> ........
> I have to calculate the numbers between a and b AND between c and d.
> So when I type "list = numbers(2, 5, 1, 4)" a list like this should appear:
>
> 2 1
> 3 2
> 4 3
> 5 4
> ........

  Susan, you state: "I have to calculate the numbers between a and b AND between c and d." The question in my mind is the meaning of this sentence. Does the 'AND' refer to the calculation or to the numbers? In other words 1) are you supposed to calculate all numbers between a and b and also calculate all numbers between c and d (which is what you seem to be striving for), or 2) does it mean you are to calculate all numbers which lie both between a and b and also between c and d (which seems to me to be the more probable meaning)? There is an important difference between these two interpretations.

  In your example it would be the difference between giving the answer you presented:

2 1
3 2
4 3
5 4

and giving the answer:

2
3
4

where these are the numbers which lie both between 2 and 5 as well as between 1 and 4 - that is, the numbers common to the two lists above.

  For the latter meaning I suggest you make appropriate use of the matlab functions 'max' and 'min', as well as the colon operator.

Roger Stafford

Subject: numbers

From: Susan Montag

Date: 12 Mar, 2010 16:38:21

Message: 13 of 17

"Matt Fig" <spamanon@yahoo.com> wrote in message <hndogj$h3j$1@fred.mathworks.com>...
> [(2:5).' (1:4).']

I've put "list = [(i1:i2).' (j1:j2).']" in my code but when I type for instance "list = numbers(1, 3, 4, 5)" in the command window I get the error:

??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Error in ==> line>local_line at 27
        lijst = [(i1:i2).' (j1:j2).'] ;

any idea how to fix that?

Subject: numbers

From: dpb

Date: 12 Mar, 2010 17:51:34

Message: 14 of 17

Susan Montag wrote:
...

> I have to calculate the numbers between a and b AND between c and d.
> So when I type "list = numbers(2, 5, 1, 4)" a list like this should appear:
>
> 2 1
> 3 2
> 4 3
> 5 4
>
>
> This is a little bit more complicated and I can't figure out how to code
> this.
...

 >> x=[2,5,1,4];
 >> l=length(x)/2;
 >> z=zeros(x(2)-x(1)+1,l);
 >> for idx=1:l, z(:,idx)=colon(x(2*idx-1),x(2*idx))';end
 >> z
z =
      2 1
      3 2
      4 3
      5 4
 >>

For the specific case -- you'll have to add in all the error checking
and so on to handle the problems you'll run into w/ a general set of
inputs that won't necessarily produce the same number of elements per
column; your input was specially constructed to do so.

In that case your only real choice would be to use cell array instead or
punt if not required to handle such inputs...

--

Subject: numbers

From: Steven Lord

Date: 12 Mar, 2010 18:17:33

Message: 15 of 17


"Susan Montag" <Sm@werew.com> wrote in message
news:hndqlt$bft$1@fred.mathworks.com...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <hndogj$h3j$1@fred.mathworks.com>...
>> [(2:5).' (1:4).']
>
> I've put "list = [(i1:i2).' (j1:j2).']" in my code but when I type for
> instance "list = numbers(1, 3, 4, 5)" in the command window I get the
> error:
>
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.
>
> Error in ==> line>local_line at 27
> lijst = [(i1:i2).' (j1:j2).'] ;
>
> any idea how to fix that?

That would happen if the number of numbers between your a and b inputs is
not the same as the number of numbers between c and d, which is indeed the
case in your situation. 1:3 contains three numbers (1, 2, and 3) while 4:5
contains 2.

In MATLAB, all rows of an array MUST contain the same number of columns and
all columns MUST contain the same number of rows (it must be "rectangular"
or the N-D equivalent.) So you have a few choices.

1) Pad the shorter arrays with some "placeholder", like 0 or NaN.
2) Perform your own error checking and throw a more specific error (using
the ERROR function) in this scenario.
3) Store your data in a cell array rather than a matrix. While the cell
array itself must be rectangular, each individual cell in the cell array can
contain whatever it wants.

A = {1:10, magic(17), single([12.5 ; 2.5; 100.5]), @cos, struct('field1',
1)}

To extract an individual cell from the cell array, use parentheses indexing:

D = A(4)

To extract _the contents of_ an individual cell from the cell array, use
curly brace indexing.

E = A{4}

I'm sure there are other options you could use -- perhaps if you explain
what you're trying to use the output to do, someone could offer other
suggestions.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: numbers

From: dpb

Date: 12 Mar, 2010 18:31:14

Message: 16 of 17

dpb wrote:
...

> >> for idx=1:l, z(:,idx)=colon(x(2*idx-1),x(2*idx))';end
...

Which, of course, could also be written as

for idx=1:l, z(:,idx)=[x(2*idx-1):x(2*idx)]'; end

that "looks more Matlab-y"...

--

Subject: numbers

From: dpb

Date: 13 Mar, 2010 05:42:53

Message: 17 of 17

dpb wrote:
> dpb wrote:
> ...
>
>> >> for idx=1:l, z(:,idx)=colon(x(2*idx-1),x(2*idx))';end
> ...
>
> Which, of course, could also be written as
>
> for idx=1:l, z(:,idx)=[x(2*idx-1):x(2*idx)]'; end
>
...

Or a tad bit cleaner on the indices...

for idx=1:2:l, z(:,idx)=[x(idx):x(idx+1)]'; end

--

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