Solved
Contributor
Posts: 33

# Adding Rows and Columns to Matrix

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
SAS Super FREQ
Posts: 4,242

## Adding Rows and Columns to Matrix

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
PROC Star
Posts: 8,164

Contributor
Posts: 33

## Adding Rows and Columns to Matrix

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?

PROC Star
Posts: 8,164

## Adding Rows and Columns to Matrix

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.

Contributor
Posts: 33

## Adding Rows and Columns to Matrix

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
SAS Super FREQ
Posts: 4,242

## Adding Rows and Columns to Matrix

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 3518 views
• 0 likes
• 3 in conversation