turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-06-2012 10:13 PM

Could someone please help me out?

I have a matrix A={1 1,1 2,2 1,2 2,2 3,3 1}. I would like to create a block diagonal matrix (B) such that B={1 1 0 0 0 0, 1 2 0 0 0 0, 0 0 2 1 0 0, 0 0 2 2 0 0, 0 0 2 3 0 0, 0 0 0 0 3 1}. I know of the block function but it does not work here (in its basic form at least). How can I do this in IML? Is it possible without looping because the number of rows in A can get very large (millions or more)?

Lots of thanks in advance for your help.

George

Accepted Solutions

Solution

10-07-2012
05:46 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KGeorge

10-07-2012 05:46 AM

You don't need to loop over rows of A; you can loop over the unique blocks defined by A. Use the UNIQUE function on the first column of A to find out how many blocks there are. Then for each block, use the BLOCK function to iteratively build up the matrix. For you example, the loop would have three iterations and be equivalent to the following statements:

B = A[1:2,]; /* initialize outside loop */

/* do i = 2 to 3; */

B = block(B, A[3:5,]);

B = block(B, A[6,]);

/* end do */

print B;

All Replies

Solution

10-07-2012
05:46 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KGeorge

10-07-2012 05:46 AM

You don't need to loop over rows of A; you can loop over the unique blocks defined by A. Use the UNIQUE function on the first column of A to find out how many blocks there are. Then for each block, use the BLOCK function to iteratively build up the matrix. For you example, the loop would have three iterations and be equivalent to the following statements:

B = A[1:2,]; /* initialize outside loop */

/* do i = 2 to 3; */

B = block(B, A[3:5,]);

B = block(B, A[6,]);

/* end do */

print B;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KGeorge

10-11-2012 02:10 PM

Thanks Rick