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:
create a list of items with pre-specified numbers

Subject: create a list of items with pre-specified numbers

From: Yihui Hung

Date: 16 Mar, 2009 08:58:10

Message: 1 of 7

Hello,

I would like to create a list of items with pre-specified numbers.

Item numbers
1 1
2 6
3 3
Given the above table, the first column is a nominal variable (item 1, item 2 and item 3) and the second column is the count numbers of the corresponding items. I’d like to create a list in which having one item 1, six item 2 and three item 3 (see below).
1
2
2
2
2
2
2
3
3
3
How can I do that? Any suggestion is appreciated. Thanks!
Sincerely,
Yihui

Subject: create a list of items with pre-specified numbers

From: Bruno Luong

Date: 16 Mar, 2009 09:43:09

Message: 2 of 7

Here are two ways to do this:

I=[1 1;
    2 6;
    3 3];

% Naive engine, easy to understand
list = arrayfun(@(i,n) i+zeros(n,1), I(:,1), I(:,2), 'uni', false)
list = cat(1, list{:})

% Vectorized, optimized for speed
cs=cumsum([1; I(:,2)]);
list=zeros(cs(end)-1,1);
list(cs(1:end-1))=diff([0; I(:,1)]);
list=cumsum(list)

% Bruno

Subject: create a list of items with pre-specified numbers

From: Roger Stafford

Date: 16 Mar, 2009 09:49:10

Message: 3 of 7

"Yihui Hung" <a489930026@yahoo.com.tw> wrote in message <gpl4b2$7sg$1@fred.mathworks.com>...
> Hello,
>
> I would like to create a list of items with pre-specified numbers.
>
> Item numbers
> 1 1
> 2 6
> 3 3
> Given the above table, the first column is a nominal variable (item 1, item 2 and item 3) and the second column is the count numbers of the corresponding items. I’d like to create a list in which having one item 1, six item 2 and three item 3 (see below).
> 1
> 2
> 2
> 2
> 2
> 2
> 2
> 3
> 3
> 3
> How can I do that? Any suggestion is appreciated. Thanks!
> Sincerely,
> Yihui

  Assume 'numbers' is a column vector of positive integers.

 p = cumsum([1;numbers]);
 list = zeros(p(numbers)-1,1);
 list(p(1:end-1)) = 1;
 list = cumsum(list);

Then 'list' would be the desired list. The quantity 'Item' is not needed in this calculation.

Roger Stafford

Subject: create a list of items with pre-specified numbers

From: us

Date: 16 Mar, 2009 10:02:09

Message: 4 of 7

"Yihui Hung"
> I would like to create a list of items with pre-specified numbers.
> Item numbers
> 1 1
> 2 6
> 3 3
> Given the above table, the first column is a nominal variable (item 1, item 2 and item 3) and the second column is the count numbers of the corresponding items. I’d like to create a list in which having one item 1, six item 2 and three item 3 (see below)...

one of the many solutions

% the data
     lst=(1:5).'; % <- the list, which should be unique...
     d=[1,1,5,1,2,2,3,3,3]; % <- the data
% the engine
% - make sure LST is sorted...
     lst=sort(lst);
     c=histc(d,lst).';
% the result
     disp([lst,c]);
%{
% lst count
     1 3
     2 2
     3 3
     4 0
     5 1
%}

us

Subject: create a list of items with pre-specified numbers

From: us

Date: 16 Mar, 2009 10:03:13

Message: 5 of 7

"Yihui Hung"
> I would like to create a list of items with pre-specified numbers.
> Item numbers
> 1 1
> 2 6
> 3 3
> Given the above table, the first column is a nominal variable (item 1, item 2 and item 3) and the second column is the count numbers of the corresponding items. I’d like to create a list in which having one item 1, six item 2 and three item 3 (see below)...

one of the many solutions

% the data
     lst=(1:5).'; % <- the list, which should be unique...
     d=[1,1,5,1,2,2,3,3,3]; % <- the data
% the engine
% - make sure LST is sorted...
     lst=sort(lst);
     c=histc(d,lst).';
% the result
     disp([lst,c]);
%{
% lst count
     1 3
     2 2
     3 3
     4 0
     5 1
%}

us

Subject: create a list of items with pre-specified numbers

From: us

Date: 16 Mar, 2009 10:03:14

Message: 6 of 7

"Yihui Hung"
> I would like to create a list of items with pre-specified numbers.
> Item numbers
> 1 1
> 2 6
> 3 3
> Given the above table, the first column is a nominal variable (item 1, item 2 and item 3) and the second column is the count numbers of the corresponding items. I’d like to create a list in which having one item 1, six item 2 and three item 3 (see below)...

one of the many solutions

% the data
     lst=(1:5).'; % <- the list, which should be unique...
     d=[1,1,5,1,2,2,3,3,3]; % <- the data
% the engine
% - make sure LST is sorted...
     lst=sort(lst);
     c=histc(d,lst).';
% the result
     disp([lst,c]);
%{
% lst count
     1 3
     2 2
     3 3
     4 0
     5 1
%}

us

Subject: create a list of items with pre-specified numbers

From: Roger Stafford

Date: 16 Mar, 2009 10:19:16

Message: 7 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpl7am$7b2$1@fred.mathworks.com>...
> Assume 'numbers' is a column vector of positive integers.
>
> p = cumsum([1;numbers]);
> list = zeros(p(numbers)-1,1);
> list(p(1:end-1)) = 1;
> list = cumsum(list);
>
> Then 'list' would be the desired list. The quantity 'Item' is not needed in this calculation.
>
> Roger Stafford

  Oops! I see that 1) there is a mistake in the second line; it should be:

 list = zeros(p(end)-1,1);

and 2) that Bruno is way ahead of me.

Roger Stafford

Tags for 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