Author Topic: Finding the min of a matrix  (Read 3944 times)

0 Members and 1 Guest are viewing this topic.

Offline Radical Pi

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1143
  • Rating: +5/-2
    • View Profile
    • RealityRevolution
Finding the min of a matrix
« on: March 10, 2010, 03:15:40 pm »
While trying to finish my simple Connect 4 game to detect tied games (aka the board matrix is all nonzero), I realized that min( isn't overloaded to handle matrices, so I'm in need of some ingenuity. Since I realized I only have to care about the min of the top row, my current solution is this:
Matr>list([A]T,L1
min(L1

But I want to know if there's a better way to do this. And I also want to know how to do the general case of finding the minimum of an entire matrix rather than just the top row like I did.

EDIT: Actually my current idea doesn't work either. Now I'm utterly clueless.

EDIT2: lol it's min(L1, not min(Ans. Disregard my last edit.
« Last Edit: March 10, 2010, 03:23:42 pm by Nyrax »
One of these days I'll get a sig I'm really proud of.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Finding the min of a matrix
« Reply #1 on: March 10, 2010, 03:45:29 pm »
mhmm at the moment I can't really figure out a fast way, but it seems like you need to convert your matrix to a list to use Min(. In this case, the only faster solution I can think about is to simply convert the game to use lists instead of matrices, but idk how well it would work and if it's faster/smaller.

Btw some people use lists instead of matrices in their games. For example in a 12x8 tilemap engine, instead of [A](A,B) they would do L1(1+B+12A), but so you can do a lot of stuff with lists, but as you can see, it increses the program size. Also I couldn't find anything on TIBD regarding the usage of Min( with matrices x.x

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Finding the min of a matrix
« Reply #2 on: March 10, 2010, 05:13:29 pm »
Matrices can't be min(ed. :(
It's too bad, as I know I've wanted to do that from time to time.
I think the solution you found is the smallest. :)
I'm not sure, but this may be faster:
[A](1,1
For(A,2,7 //I'm assuming the matrix is 7 columns. :D
min(Ans,[A](1,A
End
I don't know which way is faster.  Good luck getting it to work! ;D

Offline Radical Pi

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1143
  • Rating: +5/-2
    • View Profile
    • RealityRevolution
Re: Finding the min of a matrix
« Reply #3 on: March 10, 2010, 05:27:48 pm »
Well I just decided to use a turn counter in my C4 program, but this is still an interesting problem.

Your pseudo-recursive solution is nice, albeit big, but it can easily be extended to find the min of any matrix in general:
assuming the dimensions of the matrix are {R,C}...

R->dim(L1
For(B,1,R
[A](B,1
For(A,2,C
min(Ans,[A](B,A->L1(B
End
End
min(L1

But I still hate how there doesn't look to be an elegant way to do this :(
One of these days I'll get a sig I'm really proud of.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Finding the min of a matrix
« Reply #4 on: March 10, 2010, 05:32:58 pm »
But I still hate how there doesn't look to be an elegant way to do this :(
How about this? (Still assuming R and C are already set)
[A](1,1
For(A,1,R
For(B,1,C
min(Ans,[A](A,B
End
End


I this slightly more elegant?
« Last Edit: March 10, 2010, 05:33:17 pm by ztrumpet »

Offline Radical Pi

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1143
  • Rating: +5/-2
    • View Profile
    • RealityRevolution
Re: Finding the min of a matrix
« Reply #5 on: March 10, 2010, 05:35:33 pm »
That's much more elegant :P
*saves routine for later use*
One of these days I'll get a sig I'm really proud of.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Finding the min of a matrix
« Reply #6 on: March 10, 2010, 11:21:35 pm »
keep in mind, though, that in BASIC, the code being elegant doesn't really matter, though. People go for the fastest or smallest possible way regardless of how weird the code will look like. All that count is either optimizing for speed or for size.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Finding the min of a matrix
« Reply #7 on: March 10, 2010, 11:31:39 pm »
I just optimised for speed and size :P

Code: [Select]
e9
For(X,1,C
Matr>list([A],X,L1
min(Ans,min(L1
End

;)
« Last Edit: March 10, 2010, 11:42:30 pm by Builderboy »

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Finding the min of a matrix
« Reply #8 on: March 10, 2010, 11:37:51 pm »
You could also replace the [A](1,1 line with any value that is guaranteed to be higher than all elements of the matrix
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Finding the min of a matrix
« Reply #9 on: March 10, 2010, 11:42:06 pm »
true, so i guess e9 would be the safest and most memory efficient bet ;D

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Finding the min of a matrix
« Reply #10 on: March 11, 2010, 05:58:47 pm »
That's really nice Builderboy!  Great code! ;D