"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
> "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