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

Subject: Sequence of numbers

From: dpb

Date: 29 Oct, 2013 00:05:40

Message: 1 of 7

This is an example of the result I wish to achieve:

[2 2 2 3 3 3 4 4 4]

More generally, I wish to create a vector starting at min and finishing
at max, displaying n numbers of each value, with increments, inc, of 1.

In the above example, min = 2, max = 4, n = 3, and inc = 1.

Can this be done without looping?

yes...

--

Subject: Sequence of numbers

From: Ulrik Nash

Date: 29 Oct, 2013 00:55:07

Message: 2 of 7

dpb <none@non.net> wrote in message <l4mu44$lbi$1@speranza.aioe.org>...
> This is an example of the result I wish to achieve:
>
> [2 2 2 3 3 3 4 4 4]
>
> More generally, I wish to create a vector starting at min and finishing
> at max, displaying n numbers of each value, with increments, inc, of 1.
>
> In the above example, min = 2, max = 4, n = 3, and inc = 1.
>
> Can this be done without looping?
>
> yes...
>
> --
Okay then, prove it :-)

(perhaps I was asking for trouble)

Subject: Sequence of numbers

From: dpb

Date: 29 Oct, 2013 02:16:55

Message: 3 of 7

On 10/28/2013 7:55 PM, Ulrik Nash wrote:
> dpb <none@non.net> wrote in message <l4mu44$lbi$1@speranza.aioe.org>...
>> This is an example of the result I wish to achieve:
>>
>> [2 2 2 3 3 3 4 4 4]
>>
>> More generally, I wish to create a vector starting at min and finishing
>> at max, displaying n numbers of each value, with increments, inc, of 1.
>>
>> In the above example, min = 2, max = 4, n = 3, and inc = 1.
>>
>> Can this be done without looping?
>>
>> yes...
>>
>> --
> Okay then, prove it :-)
>
> (perhaps I was asking for trouble)

One way...

I was just answering your question--you didn't ask "how"... :)

 >> n1=1;n2=3;rep=3;
 >> cell2mat(accumarray([1:3]',[n1:n2]',[],@(x,y) {repmat(x,rep,1)}))'
ans =
      1 1 1 2 2 2 3 3 3
 >>


--

Subject: Sequence of numbers

From: TideMan

Date: 29 Oct, 2013 02:23:06

Message: 4 of 7

On Tuesday, October 29, 2013 3:16:55 PM UTC+13, dpb wrote:
> On 10/28/2013 7:55 PM, Ulrik Nash wrote:
>
> > dpb <none@non.net> wrote in message <l4mu44$lbi$1@speranza.aioe.org>...
>
> >> This is an example of the result I wish to achieve:
>
> >>
>
> >> [2 2 2 3 3 3 4 4 4]
>
> >>
>
> >> More generally, I wish to create a vector starting at min and finishing
>
> >> at max, displaying n numbers of each value, with increments, inc, of 1.
>
> >>
>
> >> In the above example, min = 2, max = 4, n = 3, and inc = 1.
>
> >>
>
> >> Can this be done without looping?
>
> >>
>
> >> yes...
>
> >>
>
> >> --
>
> > Okay then, prove it :-)
>
> >
>
> > (perhaps I was asking for trouble)
>
>
>
> One way...
>
>
>
> I was just answering your question--you didn't ask "how"... :)
>
>
>
> >> n1=1;n2=3;rep=3;
>
> >> cell2mat(accumarray([1:3]',[n1:n2]',[],@(x,y) {repmat(x,rep,1)}))'
>
> ans =
>
> 1 1 1 2 2 2 3 3 3
>
> >>
>
>
>
>
>
> --

No, dpb, that's way to complicated, try this:
v=ones(n,1)*[min:inc:max];
v=v(:);

Subject: Sequence of numbers

From: dpb

Date: 29 Oct, 2013 03:01:36

Message: 5 of 7

On 10/28/2013 9:23 PM, TideMan wrote:
> On Tuesday, October 29, 2013 3:16:55 PM UTC+13, dpb wrote:
>> On 10/28/2013 7:55 PM, Ulrik Nash wrote:
>>
>>> dpb<none@non.net> wrote in message<l4mu44$lbi$1@speranza.aioe.org>...
>>
>>>> This is an example of the result I wish to achieve:
>>
>>>>
>>
>>>> [2 2 2 3 3 3 4 4 4]
>>
>>>>
>>
>>>> More generally, I wish to create a vector starting at min and finishing
>>
>>>> at max, displaying n numbers of each value, with increments, inc, of 1.
>>
>>>>
>>
>>>> In the above example, min = 2, max = 4, n = 3, and inc = 1.
>>
>>>>
>>
>>>> Can this be done without looping?
>>
>>>>
>>
>>>> yes...
>>
>>>> --
>>
>>> Okay then, prove it :-)
>>>
>>> (perhaps I was asking for trouble)
>>
>> One way...
>>
>> I was just answering your question--you didn't ask "how"... :)
>>
>> >> n1=1;n2=3;rep=3;
>> >> cell2mat(accumarray([1:3]',[n1:n2]',[],@(x,y) {repmat(x,rep,1)}))'
>> ans =
>> 1 1 1 2 2 2 3 3 3

> No, dpb, that's way to complicated, try this:
> v=ones(n,1)*[min:inc:max];
> v=v(:);

Hey, what is this, golf?????
Note I didn't say it was a _good_ way, just one... :)

--

Subject: Sequence of numbers

From: Bruno Luong

Date: 29 Oct, 2013 06:09:06

Message: 6 of 7

dpb <none@non.net> wrote in message <l4mu44$lbi$1@speranza.aioe.org>...
> This is an example of the result I wish to achieve:
>
> [2 2 2 3 3 3 4 4 4]
>
> More generally, I wish to create a vector starting at min and finishing
> at max, displaying n numbers of each value, with increments, inc, of 1.
>
> In the above example, min = 2, max = 4, n = 3, and inc = 1.
>

reshape(repmat(min:inc:max,[n 1]),1,[])

% Bruno

Subject: Sequence of numbers

From: Ulrik Nash

Date: 29 Oct, 2013 10:16:05

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <l4nje2$1s$1@newscl01ah.mathworks.com>...
> dpb <none@non.net> wrote in message <l4mu44$lbi$1@speranza.aioe.org>...
> > This is an example of the result I wish to achieve:
> >
> > [2 2 2 3 3 3 4 4 4]
> >
> > More generally, I wish to create a vector starting at min and finishing
> > at max, displaying n numbers of each value, with increments, inc, of 1.
> >
> > In the above example, min = 2, max = 4, n = 3, and inc = 1.
> >
>
> reshape(repmat(min:inc:max,[n 1]),1,[])
>
> % Bruno

Thank you very much you three for answering my question. Great service, that is one of the reasons I love Matlab - not just a program...

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