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

Showing results for

Find a Community

- 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

12-10-2011 08:58 PM

Hi everyone!

I am looking for a way in IML to add additional rows and columns to a matrix. To give you a an idea I'd better use an example:

Let's assume I have 10 x 10 matrix. My goal is to add an extra column with 0's between the original second and third column and between the sixth and seventh column. The same goes for the rows. The result should be a 12 x 12 matrix.

Do you have any ideas?

THANK YOU!!!

Accepted Solutions

Solution

06-30-2017
03:16 PM

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

Posted in reply to MarkGIP

12-12-2011 05:24 AM

You could use the INSERT function four times, but I think it will be more efficient to allocate a 12x12 matrix of zeros and then insert the original matrix into the appropriate rows/cols as a submatrix. Both methods are shown below:

proc iml;

x = shape(1:100, 10);

/* 1. Insert row or col of zeros */

z = j(1, 10, 0); /* row of zeros */

tmp = insert(x, z, 3);

tmp = insert(tmp, z, 8);

tmp = insert(x, z`, 0, 3);

y = insert(tmp, z`, 0, 8);

print y;

/* 2. Allocate 12x12 matrix of zeros and copy x into it */

y = j(12, 12, 0); /* all zeros */

idx = setdif(1:12, {3 8}); /* gives {1 2 4 5 6 7 9 10 11 12} */

y[idx, idx] = x;

print y;

All Replies

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

Posted in reply to MarkGIP

12-11-2011 10:51 AM

Couldn't you just use the insert function? Take a look at: http://support.sas.com/documentation/cdl/en/imlug/59656/HTML/default/viewer.htm#langref_sect142.htm

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

Posted in reply to art297

12-11-2011 11:42 AM

Perfect.... Thank you so much.

Just one more thing:

Do you know a way to use the insert function for more than one vector/matrix at once. The problem is that the function won't allow me to insert the vector at two different positions at the same time (between second and third column and sixth and seventh column). The only way I can think of is using a loop.

Do you happen to know a more elegant/efficient way?

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

Posted in reply to MarkGIP

12-11-2011 11:49 AM

My knowledge of IML is EXTREMELY limited but, from what I can tell, you are simply trying to insert a 1 column matrix into an existing matrix twice. Couldn't you just do that by using two statements that use the input function with each specifying the desired column. Of course, the desired column number for the second insert would have to be one greater since a new column had already been added.

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

Posted in reply to art297

12-11-2011 11:52 AM

Thank you for your quick reply...

Repeating the statements would be also an option. However when the matrices become bigger and when there are more columns to be inserted it doesn't seem to be so efficient.

Solution

06-30-2017
03:16 PM

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

Posted in reply to MarkGIP

12-12-2011 05:24 AM

You could use the INSERT function four times, but I think it will be more efficient to allocate a 12x12 matrix of zeros and then insert the original matrix into the appropriate rows/cols as a submatrix. Both methods are shown below:

proc iml;

x = shape(1:100, 10);

/* 1. Insert row or col of zeros */

z = j(1, 10, 0); /* row of zeros */

tmp = insert(x, z, 3);

tmp = insert(tmp, z, 8);

tmp = insert(x, z`, 0, 3);

y = insert(tmp, z`, 0, 8);

print y;

/* 2. Allocate 12x12 matrix of zeros and copy x into it */

y = j(12, 12, 0); /* all zeros */

idx = setdif(1:12, {3 8}); /* gives {1 2 4 5 6 7 9 10 11 12} */

y[idx, idx] = x;

print y;