Turn on suggestions

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

Showing results for

Options

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

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 04-22-2016 08:51 AM
(1413 views)

I have a n*n matrix A and I would like to calculate A2=A*A , A3=A2*A, A4=A3*A ... A60=A59*A. Can any one help me writing a do loop to perform this task ? I have never used IML before. I do have a piece of code to complete the same task in R.

Thank you !

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

As I said when you asked this question on my blog, initialize the result matrix (P) to the identity matrix. Then loop, each time multiplying the current value of P times the original matrix. The other computations just "go along for the ride."

```
proc iml;
X = {1 2 3,
-1 0 1,
2 1 0};
V = {-1, 0, 1};
m = nrow(V);
P = I(m);
do i = 1 to 10;
P = P*X;
C = P*V;
D = V+C;
F = D*m;
end;
/* at end of loop, P = X**10 */
print P C D F;
```

10 REPLIES 10

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

You could use the matrix power operator ** to achieve this. For example:

```
a = 1.01 # i(3);
b = a**60;
print a, b;
```

Or is it important to retain all the lower powers of a?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi IanWakeling,

I would have to use all the 60 matrices for some other calculations. Having said, I need A2, A3.. A60 to perform one more computation.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Unless n is very large, and efficiency is important, then I would be tempted to calculate the powers as and when you need them. i.e.

c = a**10 + a**20;

It is is much more difficult if you want to save the whole sequence of matrices A1, A2,A3, etc...

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

If you want to store all the matrices, then I think you will have to stack them together inside one large matrix. This complicates things as you need to keep track of where in the larger matrix, the individual matrices are stored. Here is an example:

```
n = 3;
maxp = 60;
ap = j(maxp#n, n); /* large matrix in which to keep all the powers of a */
ri = shape(1:(maxp#n), maxp); /* row index matrix for sub-matrices in ap */
a = 1.01 # i(n); /* define the matrix a */
ap[ ri[ 1, ], ] = a; /* write matrix a to ap */
do i = 2 to maxp; /* write powers to ap */
ap[ ri[ i, ], ] = ap [ ri[ i-1, ], ] * a;
end;
c = ap[ ri[ 3, ], ]; /* set c to to a**3 */
```

Hope that helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Do You want create many Matrix A2-A60 to represent that calculation?

proc iml; call randseed(1234); A=j(4,4); call randgen(A,'uniform'); x={'A'}+left(char(2:60)); B=A; do i=1 to 59; B=B*A; call valset(x[i],B); end; print A; do i=1 to 59; temp=value(x[i]); label=x[i]; print temp[l=label]; end; quit;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The use of the VALSET and VALUE functions are described in the article "Indirect assignment." However, it is rare to need to use this technique. Almost always, you can avoid indirect assignment and use the matrices inside the DO loop without saving them into 60 different named matrices.

What are you trying to acheive? That would help us know what programming technique to suggest.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you Rick,

This is what I am trying to accomplish

I have two matrices

P=mXm

V=mX1

and a saclar value m

I am looking for a sas iml code to do the following

Run a loop for 10 times say, to get the final result of F.

where P=P at the befining for second run it should be p=P*P, and at the end (for 10) is should be

p=p*p*p*p*p*p*p*p*p*p

C=P*v;

D=V+C;

F=D*m;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

As I said when you asked this question on my blog, initialize the result matrix (P) to the identity matrix. Then loop, each time multiplying the current value of P times the original matrix. The other computations just "go along for the ride."

```
proc iml;
X = {1 2 3,
-1 0 1,
2 1 0};
V = {-1, 0, 1};
m = nrow(V);
P = I(m);
do i = 1 to 10;
P = P*X;
C = P*V;
D = V+C;
F = D*m;
end;
/* at end of loop, P = X**10 */
print P C D F;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you Rick!

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.