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:
Is there a better way to create this repetitive array?

Subject: Is there a better way to create this repetitive array?

From: Kuroro Lam

Date: 12 May, 2010 19:13:04

Message: 1 of 8

I want to create an array that looks like this:

ids = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ... N, N, N, N]

Right now I'm using a for loop between 1 and N and concatenating each set of numbers on to the array.

ids = []
for i=1:N
ids = [ids ones(1,6)*i]
end

This works perfectly fine, I'm just wondering if there might be better way of doing it.

Subject: Is there a better way to create this repetitive array?

From: Jos (10584)

Date: 12 May, 2010 19:23:06

Message: 2 of 8

"Kuroro Lam" <v5lam@engmail.uwaterloo.ca> wrote in message <hseuk0$9c8$1@fred.mathworks.com>...
> I want to create an array that looks like this:
>
> ids = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ... N, N, N, N]
>
> Right now I'm using a for loop between 1 and N and concatenating each set of numbers on to the array.
>
> ids = []
> for i=1:N
> ids = [ids ones(1,6)*i]
> end
>
> This works perfectly fine, I'm just wondering if there might be better way of doing it.

Your current for-loop is among the worst options, as it increases the array in every iteration. Here are two of the many other ways to get the same matrix

N = 5 ;
m = 3 ;

ids1 = ceil(1:(m*N)/m)
ids2 = reshape(repmat(1:N,m,1),1,[])

hth
Jos

Subject: Is there a better way to create this repetitive array?

From: Ashish Uthama

Date: 12 May, 2010 19:24:58

Message: 3 of 8

On Wed, 12 May 2010 15:13:04 -0400, Kuroro Lam
<v5lam@engmail.uwaterloo.ca> wrote:

> I want to create an array that looks like this:
>
> ids = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6,
> 6, 6 ... N, N, N, N]
>
> Right now I'm using a for loop between 1 and N and concatenating each
> set of numbers on to the array.
>
> ids = []
> for i=1:N
> ids = [ids ones(1,6)*i]
> end
>
> This works perfectly fine, I'm just wondering if there might be better
> way of doing it.



N=10;P=4;

%%
reps=repmat(1:N,[P 1]);
reps=reps(:)';


%% Or,
reps = ceil([1:N*P]./P);

Subject: Is there a better way to create this repetitive array?

From: Matt Fig

Date: 12 May, 2010 19:39:20

Message: 4 of 8

And another few alternatives, just for kicks:

% Data
N = 5 ;
m = 4 ;


% Engine 1
ids1 = expand(ceil(1:(m*N)/m)1,[1,m]) % EXPAND on the FEX

% Engine 2
ids1 = zeros(1,N*m);
ids1(1:m:end-1) = 1;
ids1 = cumsum(ids1)

Subject: Is there a better way to create this repetitive array?

From: Kuroro

Date: 12 May, 2010 19:45:21

Message: 5 of 8

Awesome! Thanks everyone.

Subject: Is there a better way to create this repetitive array?

From: Sean

Date: 12 May, 2010 19:50:21

Message: 6 of 8

"Kuroro Lam" <v5lam@engmail.uwaterloo.ca> wrote in message <hseuk0$9c8$1@fred.mathworks.com>...
> I want to create an array that looks like this:
>
> ids = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ... N, N, N, N]


>>ids = kron(1:N,ones(1,4))

Subject: Is there a better way to create this repetitive array?

From: Matt J

Date: 12 May, 2010 20:30:25

Message: 7 of 8

"Kuroro Lam" <v5lam@engmail.uwaterloo.ca> wrote in message <hseuk0$9c8$1@fred.mathworks.com>...

> I want to create an array that looks like this:
>
> ids = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ... N, N, N, N]
=======

Seems like a bad idea as it generates a lot of redundant data. What do you need it for?

Subject: Is there a better way to create this repetitive array?

From: Loren Shure

Date: 13 May, 2010 11:00:24

Message: 8 of 8

In article <hseuk0$9c8$1@fred.mathworks.com>, v5lam@engmail.uwaterloo.ca
says...
> I want to create an array that looks like this:
>
> ids = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ... N, N, N, N]
>
> Right now I'm using a for loop between 1 and N and concatenating each set of numbers on to the array.
>
> ids = []
> for i=1:N
> ids = [ids ones(1,6)*i]
> end
>
> This works perfectly fine, I'm just wondering if there might be better way of doing it.
>

Do you really need this expanded array, or could you work with 1:N and
do what you need with bsxfun? If so, it could be more memory-efficient.

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

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