BookmarkSubscribeRSS Feed
arbongard
Calcite | Level 5

How would I write a function module that will input 3 identical sized matrices (not knowing the size) and retunr the one that has the largest determinant.  The function should check that the matrices are square and all the same size.

5 REPLIES 5
IanWakeling
Barite | Level 11

How about the following?

.

start maxdet(a, b, c);

  nr = nrow(a) || nrow(b) || nrow(c);

  nc = ncol(a) || ncol(b) || ncol(c);

  if all(nr=nc) & max(nr)=min(nr) then do;

    i = ( det(a) || det(b) || det(c) )[<:>];

    if i = 1 then return(a);

      else if i = 2 then return(b);

      else if i = 3 then return(c);

  end; else return(.);

finish;

.

Not sure why you want to do this.   What if two matrices are tied for the largest determinant?

arbongard
Calcite | Level 5

I am trying to figure that out.

I think I would want to print both of them if they were tied to show that they had a tie. how would I do that?
also how would I make the matrix or matrices show up in the output log.

IanWakeling
Barite | Level 11

If you get the three determinants into a vector called d, then you could use syntax like  i = loc(d=max(d)) to get the index numbers of all determinants equal to the max.   You could then adapt the if statements at the end along the lines of  if any( i =1) then print a;   etc...

arbongard
Calcite | Level 5

so instead of:

i = ( det(a) || det(b) || det(c) )[<:>];
I should use:

i = loc(d=max(d));  This would give me the max det value or would this give me the matrix with max determinate? 
I want the print out the matrix (just the matrix) with the max determinate.

Every thing is fine after the then do statement.

IanWakeling
Barite | Level 11

It will give you the index number of the matrix with the max determinant.  I suggest you look into the LOC() function as it has many uses.  Either look in the documentation or read Rick's excellent blog, he has written about LOC several times:

Beware the naked LOC - The DO Loop

LOC: The most useful function you&#8217;ve never heard of - The DO Loop

I suggest you replace the old definition of i, with:

  d = det(a) || det(b) || det(c);
  i = loc( d = max(d));

Note there is no nakedness here as the vector of three determinants (d) must have at least one element equal to the maximum.  You now need to think about handling ties. Most of the time 'i' will be a 1x1 matrix containing the number 1,2 or 3 of the matrix a,b or c with the max determinant.  But it might be a 1x2 or a 1x3 matrix of index numbers if there are ties.  You need to add code to handle these different conditions, perhaps print a warning message about the number of ties.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1090 views
  • 3 likes
  • 2 in conversation