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:
Equivalent of colon operator to increment a vector

Subject: Equivalent of colon operator to increment a vector

From: Alexandru

Date: 5 Jun, 2013 19:58:12

Message: 1 of 6

Hello,

I know one can do 1:5 to generate the vector with entries 1 2 3 4 5.
Suppose I want to do the same but starting from a vector, i.e. do [1; 2; 3] : [5; 6; 7] to get a 3 by 5 matrix:

1 2 3 4 5
2 3 4 5 6
3 4 5 6 7

This doesn't work in Matlab as I only get the first line. I know I could do a for loop etc, but is there a direct way of doing this (avoiding loops)?

Thanks,
Alex

Subject: Equivalent of colon operator to increment a vector

From: Alexandru

Date: 5 Jun, 2013 20:05:10

Message: 2 of 6

I know I could do [1:5; 2:6; 3:7] but that is not what I want. The starting vector is the output of some other code. Its elements are not consecutive and its length is varying as well.

Subject: Equivalent of colon operator to increment a vector

From: Alexandru

Date: 5 Jun, 2013 20:09:10

Message: 3 of 6

"Alexandru" wrote in message <koo58k$hsd$1@newscl01ah.mathworks.com>...
> Hello,
>
> I know one can do 1:5 to generate the vector with entries 1 2 3 4 5.
> Suppose I want to do the same but starting from a vector, i.e. do [1; 2; 3] : [5; 6; 7] to get a 3 by 5 matrix:
>
> 1 2 3 4 5
> 2 3 4 5 6
> 3 4 5 6 7
>
> This doesn't work in Matlab as I only get the first line. I know I could do a for loop etc, but is there a direct way of doing this (avoiding loops)?
>
> Thanks,
> Alex

I know that I can simply do [1:5; 2:6; 3:7] but that's not what I want. The starting vector will be the output of previous code. Its elements will not be consecutive and its size is not fixed.

Subject: Equivalent of colon operator to increment a vector

From: Alexandru

Date: 5 Jun, 2013 20:17:15

Message: 4 of 6

Never mind, I found a work around

v*ones(1,5)+ones(5,1)*[0:4]

where v is my original vector.

Subject: Equivalent of colon operator to increment a vector

From: James Tursa

Date: 5 Jun, 2013 20:20:09

Message: 5 of 6

"Alexandru" wrote in message <koo5t6$jur$1@newscl01ah.mathworks.com>...
> "Alexandru" wrote in message <koo58k$hsd$1@newscl01ah.mathworks.com>...
> > Hello,
> >
> > I know one can do 1:5 to generate the vector with entries 1 2 3 4 5.
> > Suppose I want to do the same but starting from a vector, i.e. do [1; 2; 3] : [5; 6; 7] to get a 3 by 5 matrix:
> >
> > 1 2 3 4 5
> > 2 3 4 5 6
> > 3 4 5 6 7
> >
> > This doesn't work in Matlab as I only get the first line. I know I could do a for loop etc, but is there a direct way of doing this (avoiding loops)?
> >
> > Thanks,
> > Alex
>
> I know that I can simply do [1:5; 2:6; 3:7] but that's not what I want. The starting vector will be the output of previous code. Its elements will not be consecutive and its size is not fixed.

It is not clear to me if you have two vectors that you are working with, or just a desired length for the output. In any event, here is one way to do it:

>> a = [1;3;7] % not consecutive
a =
     1
     3
     7
>> b = a + 4
b =
     5
     7
    11
>> n = b(1)-a(1) % dynamically calculated length
n =
     4
>> bsxfun(@plus,a,0:n)
ans =
     1 2 3 4 5
     3 4 5 6 7
     7 8 9 10 11

James Tursa

Subject: Equivalent of colon operator to increment a vector

From: Alexandru

Date: 5 Jun, 2013 22:42:07

Message: 6 of 6

"James Tursa" wrote in message <koo6hp$m33$1@newscl01ah.mathworks.com>...
> "Alexandru" wrote in message <koo5t6$jur$1@newscl01ah.mathworks.com>...
> > "Alexandru" wrote in message <koo58k$hsd$1@newscl01ah.mathworks.com>...
> > > Hello,
> > >
> > > I know one can do 1:5 to generate the vector with entries 1 2 3 4 5.
> > > Suppose I want to do the same but starting from a vector, i.e. do [1; 2; 3] : [5; 6; 7] to get a 3 by 5 matrix:
> > >
> > > 1 2 3 4 5
> > > 2 3 4 5 6
> > > 3 4 5 6 7
> > >
> > > This doesn't work in Matlab as I only get the first line. I know I could do a for loop etc, but is there a direct way of doing this (avoiding loops)?
> > >
> > > Thanks,
> > > Alex
> >
> > I know that I can simply do [1:5; 2:6; 3:7] but that's not what I want. The starting vector will be the output of previous code. Its elements will not be consecutive and its size is not fixed.
>
> It is not clear to me if you have two vectors that you are working with, or just a desired length for the output. In any event, here is one way to do it:
>
> >> a = [1;3;7] % not consecutive
> a =
> 1
> 3
> 7
> >> b = a + 4
> b =
> 5
> 7
> 11
> >> n = b(1)-a(1) % dynamically calculated length
> n =
> 4
> >> bsxfun(@plus,a,0:n)
> ans =
> 1 2 3 4 5
> 3 4 5 6 7
> 7 8 9 10 11
>
> James Tursa

Thanks James!

Tags for 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