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:
use a colon operator for all elements of row vectors

Subject: use a colon operator for all elements of row vectors

From: Misha Koshelev

Date: 24 May, 2009 17:19:02

Message: 1 of 5

Hi, I am trying to do one of the following equivalent things (well equivalent for this problem):

1) I have row vectors
a = [1;2;3]; b = [4;5;6] and I want to get a (cell?) array like
{[1 2 3 4] [2 3 4 5] [3 4 5 6]}
in the fastest possible way without using a for loop?

2) I have a row vector again
a = [20; 21; 22; 27; 28; 30; 31; 32; 33];
And I want to "fill in" the elements in a new row vector so that, let's say, this vector will have all elements from a to a+T. Let's say for T=2 I want:
newvec = [20; 21; 22; 23; 24; 27; 28; 29; 30; 31; 32; 33; 34; 35].

Now I actually also want to limit the elements that get "expanded" so that we do not go past a module 10 boundary (e.g., 29 will not be expanded; 28 will only be expanded to 29; 27 to 28 and 29; 26 to 27 and 28)...

Thank you for your help.

Misha

Subject: use a colon operator for all elements of row vectors

From: Misha Koshelev

Date: 24 May, 2009 20:27:01

Message: 2 of 5

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gvbvi6$h6g$1@fred.mathworks.com>...
> Hi, I am trying to do one of the following equivalent things (well equivalent for this problem):
>
> 1) I have row vectors
> a = [1;2;3]; b = [4;5;6] and I want to get a (cell?) array like
> {[1 2 3 4] [2 3 4 5] [3 4 5 6]}
> in the fastest possible way without using a for loop?
>
> 2) I have a row vector again
> a = [20; 21; 22; 27; 28; 30; 31; 32; 33];
> And I want to "fill in" the elements in a new row vector so that, let's say, this vector will have all elements from a to a+T. Let's say for T=2 I want:
> newvec = [20; 21; 22; 23; 24; 27; 28; 29; 30; 31; 32; 33; 34; 35].
>
> Now I actually also want to limit the elements that get "expanded" so that we do not go past a module 10 boundary (e.g., 29 will not be expanded; 28 will only be expanded to 29; 27 to 28 and 29; 26 to 27 and 28)...
>
> Thank you for your help.
>
> Misha

Ok, here's something simpler. I want to vectorize something like this:

a = [row vector];
b = [];
for i=1:K
    b = union(b,a+i);
end

Subject: use a colon operator for all elements of row vectors

From: Bruno Luong

Date: 24 May, 2009 20:37:01

Message: 3 of 5

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gvcail$ht7$1@fred.mathworks.com>...

>
> a = [row vector];
> b = [];
> for i=1:K
> b = union(b,a+i);
> end

unique(bsxfun(@plus, a(:), 1:K)).'

Bruno

Subject: use a colon operator for all elements of row vectors

From: Bruno Luong

Date: 24 May, 2009 20:44:01

Message: 4 of 5

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gvbvi6$h6g$1@fred.mathworks.com>...
> Hi, I am trying to do one of the following equivalent things (well equivalent for this problem):
>
> 1) I have row vectors
> a = [1;2;3]; b = [4;5;6] and I want to get a (cell?) array like
> {[1 2 3 4] [2 3 4 5] [3 4 5 6]}
> in the fastest possible way without using a for loop?

c=arrayfun(@(l,r) (l:r), a, b, 'un', 0)'

Bruno

Subject: use a colon operator for all elements of row vectors

From: Bruno Luong

Date: 24 May, 2009 20:46:01

Message: 5 of 5

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gvbvi6$h6g$1@fred.mathworks.com>...

>
> 2) I have a row vector again
> a = [20; 21; 22; 27; 28; 30; 31; 32; 33];
> And I want to "fill in" the elements in a new row vector so that, let's say, this vector will have all elements from a to a+T. Let's say for T=2 I want:
> newvec = [20; 21; 22; 23; 24; 27; 28; 29; 30; 31; 32; 33; 34; 35].
>

See post #3.

Next.

Bruno

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