Statistical programming, matrix languages, and more

Matrices

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

Frequent Contributor
Posts: 130

Re: Matrices

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) )[<Smiley Embarassed];

    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

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.

Frequent Contributor
Posts: 130

Re: Matrices

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

so instead of:

i = ( det(a) || det(b) || det(c) )[<Smiley Embarassed];
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.

Frequent Contributor
Posts: 130

Re: Matrices

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.

Ask a Question
Discussion stats
  • 5 replies
  • 461 views
  • 3 likes
  • 2 in conversation