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:
Vector build up help

Subject: Vector build up help

From: Aron Sceidt

Date: 16 May, 2011 17:48:04

Message: 1 of 5

Is there an efficient way to build a vector like for example I need four 1's after each other, two 2's, seven 4's and three 9's? Something like the way we use the colon operator?

[1 1 1 1 2 2 4 4 4 4 4 4 4 9 9 9]

Subject: Vector build up help

From: Andy

Date: 16 May, 2011 17:57:19

Message: 2 of 5

"Aron Sceidt" <sigarru87@gmail.com> wrote in message <iqro0k$rd$1@newscl01ah.mathworks.com>...
> Is there an efficient way to build a vector like for example I need four 1's after each other, two 2's, seven 4's and three 9's? Something like the way we use the colon operator?
>
> [1 1 1 1 2 2 4 4 4 4 4 4 4 9 9 9]

Only if you can describe a consistent pattern by which you are building the vector. This seems sort of random.

Subject: Vector build up help

From: dpb

Date: 16 May, 2011 17:57:35

Message: 3 of 5

On 5/16/2011 12:48 PM, Aron Sceidt wrote:
> Is there an efficient way to build a vector like for example I need four
> 1's after each other, two 2's, seven 4's and three 9's? Something like
> the way we use the colon operator?
>
> [1 1 1 1 2 2 4 4 4 4 4 4 4 9 9 9]

Combinations of repmat(), ones(), etc., can be joined in creative ways... :)

There are other more exotic things that can be used on occasion
depending on the actual pattern(s) needed; what, specifically would be
dependent on those patterns.

[ones(1,4) 2 2 4*ones(1,7) 9 9 9]

is just one obvious of any number of possibilities for the example. One
would presume the number of repetitions and values are programmatically
know; if so, the above could be automated or at least made generic w/
use of variables instead of explicit constants.

--

Subject: Vector build up help

From: Roger Stafford

Date: 16 May, 2011 18:43:04

Message: 4 of 5

"Aron Sceidt" <sigarru87@gmail.com> wrote in message <iqro0k$rd$1@newscl01ah.mathworks.com>...
> Is there an efficient way to build a vector like for example I need four 1's after each other, two 2's, seven 4's and three 9's? Something like the way we use the colon operator?
>
> [1 1 1 1 2 2 4 4 4 4 4 4 4 9 9 9]
- - - - - - - - - - - - -
 % Given these;
 u = [1 2 4 9]; % The values to be repeated
 c = [4 2 7 3]; % Repetition counts of each value

 % Do this:
 p = cumsum([1,c]);
 v = zeros(1,p(end)-1);
 v(p(1:end-1)) = diff([0,u]);
 v = cumsum(v);

Then x would be your desired result.

Roger Stafford

Subject: Vector build up help

From: Roger Stafford

Date: 16 May, 2011 19:00:05

Message: 5 of 5

"Roger Stafford" wrote in message <iqrr7o$c45$1@newscl01ah.mathworks.com>...
> Then x would be your desired result.
- - - - - - - - -
  Correction: Then v would be your desired result.

Roger Stafford

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