## Matrices

Occasional Contributor
Posts: 8

# Matrices

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.

Regular Contributor
Posts: 168

## Re: Matrices

Posted in reply to arbongard

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?

Occasional Contributor
Posts: 8

## Re: Matrices

Posted in reply to IanWakeling

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.

Regular Contributor
Posts: 168

## Re: Matrices

Posted in reply to arbongard

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...

Occasional Contributor
Posts: 8

## Re: Matrices

Posted in reply to IanWakeling

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.

Regular Contributor
Posts: 168

## Re: Matrices

Posted in reply to arbongard

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

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.

Discussion stats
• 5 replies
• 488 views
• 3 likes
• 2 in conversation