From: Kuroro Lam on 12 May 2010 15:13 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.
From: Jos (10584) on 12 May 2010 15:23 "Kuroro Lam" <v5lam(a)engmail.uwaterloo.ca> wrote in message <hseuk0$9c8$1(a)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
From: Ashish Uthama on 12 May 2010 15:24 On Wed, 12 May 2010 15:13:04 -0400, Kuroro Lam <v5lam(a)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);
From: Matt Fig on 12 May 2010 15:39 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)
From: Kuroro on 12 May 2010 15:45 Awesome! Thanks everyone.
|
Next
|
Last
Pages: 1 2 Prev: how to break a big column matrix into 2d matrix? Next: IFontDisp Object in Matlab? |