Got Questions? Get Answers.
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:
Binary Matrix

Subject: Binary Matrix

From: prince2009

Date: 20 Jan, 2009 05:51:27

Message: 1 of 4

Hi All,
I have a table has two columns: Transaction ID and Item ID. Here is a sample of this table:

Tid itemid
111 001
112 001
113 999
114 415

I would like to create a binary matrix based on the above table data so that the matrix looks like this:

     001 999 415
111 1 0 0
112 1 0 0
113 0 1 0
114 0 0 1

Each column is an item id and each row is a transaction id. One means the a transaction purchased this item and zero means it did not purchased the item.
I want to do this so that I can apply clustering on the matrix to study the shopping patterns.
can anyone help me on how to create the binary matrix based on the table that I have please?

Subject: Binary Matrix

From: Roger Stafford

Date: 20 Jan, 2009 06:10:04

Message: 2 of 4

prince2009 <wadmansoori@yahoo.com> wrote in message <12816297.1232430717399.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hi All,
> I have a table has two columns: Transaction ID and Item ID. Here is a sample of this table:
>
> Tid itemid
> 111 001
> 112 001
> 113 999
> 114 415
>
> I would like to create a binary matrix based on the above table data so that the matrix looks like this:
>
> 001 999 415
> 111 1 0 0
> 112 1 0 0
> 113 0 1 0
> 114 0 0 1
>
> Each column is an item id and each row is a transaction id. One means the a transaction purchased this item and zero means it did not purchased the item.
> I want to do this so that I can apply clustering on the matrix to study the shopping patterns.
> can anyone help me on how to create the binary matrix based on the table that I have please?

  Check out 'accumarray'. It is just what you need.

Roger Stafford

Subject: Binary Matrix

From: Roger Stafford

Date: 20 Jan, 2009 06:37:02

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gl3prs$pdn$1@fred.mathworks.com>...
> prince2009 <wadmansoori@yahoo.com> wrote in message <12816297.1232430717399.JavaMail.jakarta@nitrogen.mathforum.org>...
> > Hi All,
> > I have a table has two columns: Transaction ID and Item ID. Here is a sample of this table:
> >
> > Tid itemid
> > 111 001
> > 112 001
> > 113 999
> > 114 415
> >
> > I would like to create a binary matrix based on the above table data so that the matrix looks like this:
> >
> > 001 999 415
> > 111 1 0 0
> > 112 1 0 0
> > 113 0 1 0
> > 114 0 0 1
> >
> > Each column is an item id and each row is a transaction id. One means the a transaction purchased this item and zero means it did not purchased the item.
> > I want to do this so that I can apply clustering on the matrix to study the shopping patterns.
> > can anyone help me on how to create the binary matrix based on the table that I have please?
>
> Check out 'accumarray'. It is just what you need.
>
> Roger Stafford

  Perhaps I should give a few more details. You also need to use 'unique' to provide for repetitions in 'itemid' and possibly in 'Tid'.

 [b1,m1,n1] = unique(Tid);
 [b2,m2,n2] = unique(itemid);
 A = accumarray([n1,n2],1);

The i-th row of A will represent the 'Tid' value = b1(n1(i)) and the j-th column the 'itemid' value = b2(n2(j)). This indirect representation is necessary since the 'Tid' and 'itemid' values are inappropriate to be indices themselves.

Roger Stafford

Subject: Binary Matrix

From: Roger Stafford

Date: 20 Jan, 2009 06:56:01

Message: 4 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gl3ree$c6h$1@fred.mathworks.com>...
> .......
> The i-th row of A will represent the 'Tid' value = b1(n1(i)) and the j-th column the 'itemid' value = b2(n2(j)).
> ........

  I erred in that last paragraph. My apologies. It should read:

The i-th row of A will correspond to the 'Tid' value = b1(i) and the j-th column the 'itemid' value = b2(j).

Roger Stafford

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