Is there a better way to create this repetitive array?

  • Follow


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.
0
Reply Kuroro 5/12/2010 7:13:04 PM

"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
0
Reply Jos 5/12/2010 7:23:06 PM


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);
0
Reply Ashish 5/12/2010 7:24:58 PM

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)
0
Reply Matt 5/12/2010 7:39:20 PM

Awesome! Thanks everyone.
0
Reply Kuroro 5/12/2010 7:45:21 PM

"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))
0
Reply Sean 5/12/2010 7:50:21 PM

"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?
0
Reply Matt 5/12/2010 8:30:25 PM

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
0
Reply Loren 5/13/2010 11:00:24 AM

7 Replies
196 Views

(page loaded in 0.103 seconds)

Similiar Articles:













7/24/2012 12:27:06 AM


Reply: