BookmarkSubscribeRSS Feed
EyalGonen
Lapis Lazuli | Level 10

Hi,

I need to calculate the difference between a row vectror and a column vector in a matrix. For example:

vr = {10 15}

vc = {5 10}

m = {5 10,

        0 5}

m = is the difference between them. m[1.1] = vr[1] - vc[1], m[1,2] = vr[2] - vc[1] etc.

Currently I am doing this using a loop. Is there a way to accomplish this using vector/matrix notation?

Thanks,

Eyal

2 REPLIES 2
IanWakeling
Barite | Level 11

The repeat function is one way of doing this.  For example:

proc iml;

  vr = {10 15};

  vc = {5, 10};

  m=repeat(vr,2,1)-repeat(vc,1,2);

  print m;

quit;

where 2 is the length of both vectors.  Note that in your example vc is not a column vector, so I have added a comma above.

Hope that helps.

Rick_SAS
SAS Super FREQ

Ian has the right idea, although you only need to use the REPEAT function once:

vr = {10 15};
vc = {5 10};

v = repeat(vr, ncol(vc)); /* repeat vector to create matrix */
m = v - vc`;  /* subtract i_th element from i_th row */
print m;

For details, see this article on "Shorthand notation for row and column operations."

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1939 views
  • 3 likes
  • 3 in conversation