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: Assignin into Matlab variable struct - comp.soft-sys.matlab ...> Is there another function or a better way to do such thing ? Why are you trying to create a struct array in the base workspace from within your script or function? ISO C++ forbids assignment of arrays - is there a way out ? - comp ...... comp.lang.idl-pvwave ISO C++ forbids assignment of arrays - is there a way out ? - comp ... find max in 3D array -- slow - comp.lang.idl-pvwave Is there a better way to ... Is there a better way to do this? - comp.lang.java.helpYou could create a array of type boolean with array indexes 0 to 49 (just don't ... Maybe there is a better way to do the multiplication (see below) and to ... Is this the best way to store my data? - comp.soft-sys.matlab ...Does doing this inside this structure incur more performance penalties than a regular stand-alone array would? More importantly, is there a better way to do this? Performance: struct vs. cell array - comp.soft-sys.matlab ...Is there a better way to do this, or should I just go with a cell array for speed? ... probably ok if the contest is fair "on average" and there ... find max in 3D array -- slow - comp.lang.idl-pvwaveIs there a better way to search for the maxima using IDL? The ... is there a way out ? - comp ... find max in 3D array -- slow - comp.lang.idl-pvwave Is there a better way ... Modifying Select Elements in Array of Objects - comp.lang.ruby ...Hi, I have an array of objects, and the ... just to select the elements I want, make the changes, and leave the rest of the elements unchanged. Is there a better way ... Changing multiple matrix element at one time - comp.soft-sys ...Another question, I have a 3D matrix, with only 2 dimension with arrays ... this works but I don't feel comfortable with this linspace thing, is there > a better way ... Switch + Case - comp.lang.asm.x86Is there a way that I can create inline Array_Function without needing to use call and ret instruction. ... will optimize switch statements to jump tables, or better ... How to repeat a vector to creat a matrix - comp.soft-sys.matlab ...... 4 5 6 7 ]; > > > > > > I can use a for loop, but I am wondering if there is a better way to ... Creating a matrix from a vector (using arrays) - comp.soft-sys.sas ... How to ... Improving Performance in Excel 2007 - Microsoft Corporation ...... increase, especially for repetitive ... involves a function or array formulas, determine if there is a more efficient way to ... The better use you make of smart recalculation ... Array data structure - Wikipedia, the free encyclopediaAn index maps the array value to a stored object. There are three ways in ... operations may create non-contiguous sub-arrays from them. There are ... factor of B/k better than ... 7/24/2012 12:27:06 AM
|