Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: cooliojazz on January 29, 2010, 10:39:27 pm

Title: Matrix Help
Post by: cooliojazz on January 29, 2010, 10:39:27 pm
OK, say I wanted to take a matrix
[1,2,3]
[4,7,3]
[9,5,6]
and take off the first row so it was
[2,3]
[7,3]
[5,6]
how could i do this?  The only way I've been able to do this would be using Matrix to list, but that takes a lot of space and is slow, so...
Title: Re: Matrix Help
Post by: ztrumpet on January 29, 2010, 11:19:35 pm
You could use a for( loop, but I have no idea if it's faster.
Input and Output in [A]
:dim([A]->L1
:For(A,1,L1(1
:For(B,2,L1(2
:[A](A,B->[A](A,B-1
:End
:End
:{L1(1),L1(2)-1->dim([A]
Title: Re: Matrix Help
Post by: Builderboy on January 30, 2010, 12:19:30 am
Hmmm, I'm tempted to do some sort of fancy list manipulations and such, but I have another idea that may or may not work.

I assume these values will be accessed at some time in the program, so what I'm thinking is instead of changing the matrix, you could have a 'shift' variable that you can change.  Instead of saying [A](A,B) you would have [A](A+S,B) where S is the shift variable.  To simulate shifting the matrix over by one, you could instead just increase S by 1.
Title: Re: Matrix Help
Post by: cooliojazz on January 30, 2010, 12:20:33 am
Hmm, i think that would be slower than
:[A]T->[A]
:Matr->Lst([A],L1,L2,L3)
:Lst->Matr(L2,L3,[A])
:[A]T->[A]
Which is what i'm doing. I could be wrong tho.

EDIT: Grr, 3 seconds BuilderBoy :P
The problem with doing that tho, is that i'm also adding a random row on the end, so the matrix woul grow to unreasonable proportions quickly. (really line 3 is :Lst->Matr(L2,L3,L1,[A]) where L1 is a new random line.) and no, those values are now null and void.
Title: Re: Matrix Help
Post by: Builderboy on January 30, 2010, 01:04:10 am
Alrighty then, what you need is a radial List!

Basicaly instead of moving the Matrix around so that the begining is at colomn 1, you move a pointer around that says where column 1 is.  Does that make sense?  I can run up some code if you like.
Title: Re: Matrix Help
Post by: cooliojazz on January 30, 2010, 01:09:06 am
Not really.  Could you give a coded example?
Title: Re: Matrix Help
Post by: Builderboy on January 30, 2010, 02:08:53 am
Sure.  I used this technique on a snake game i made.  It s prime example of how you want to move a whole bunch of elements down a list without losing much speed.

So here we have a list of 10 numbers (I'll use lists, but it still applies to matrixes)

Code: [Select]
{1,2,3,4,5,6,7,8,9,0->L1
20->S                                //We set the shift to 20.  If it goes negative, bad things happen :P
"1+10fPart((X+S)/10->Y1         //This is our magic formula, using modulus arithmetic, it gives us
                               //the element we want but accounts for the shift
                               //It is stored to Y1 for easy use :P
While 1

For(F,1,10                 //Just some simple code to display the list from 'start' to 'finish'
Output(1,F,L1(Y1(F           //But using the formula, so in perspective of the shift
End

Repeat Ans
getKey -> K
End

S + (K=24) - (k=26)    //You can change the shift with your arrow keys

End
Title: Re: Matrix Help
Post by: cooliojazz on January 30, 2010, 02:21:10 am
Well, thats useful if L1, or [A] is always the same, but what if its always changing?  Or does your code apply to that too? (I'm just not seein it man. :P)
Title: Re: Matrix Help
Post by: Builderboy on January 30, 2010, 02:42:07 am
Well let's look at it his way.  What this allows you to do is "rotate" the elements of a list through that list, and you can read and write to it normaly.  To do as you wanted, when you shifted the elements down, one would get pushed off the edge and go around to slot 10.  You can then overwrite it with your random value, and we have a scrolling list!
Title: Re: Matrix Help
Post by: cooliojazz on January 30, 2010, 03:27:38 am
Oh, ok, i see what you're saying. This would be amazing and exactly what i need... except that i cant have it wrap :(
Title: Re: Matrix Help
Post by: Galandros on January 30, 2010, 08:24:19 am
With lists, to get out the first element we use cumSum and /delta/List combo.

Can a similar thing be done in Matrices? I have to get the calc to test. I am pretty sure cumSum works with matrices but /delta/List don't.
Title: Re: Matrix Help
Post by: ztrumpet on January 30, 2010, 11:21:09 am
Builderboy, that's great code!  I've done the same thing in dodge-the-asteroids games.  I've probably made 10 of them, and they keep getting faster. :)

Right you are Galandros.  I don't know if you can manipulate it with a formula, as that's used for lists. (Galandros, I know you know that, but that's for those who didn't know that.) :)
Title: Re: Matrix Help
Post by: Builderboy on January 30, 2010, 12:48:15 pm
Oh, ok, i see what you're saying. This would be amazing and exactly what i need... except that i cant have it wrap :(

Hmmm, I'm not quite sure what you mean.  Here, take a look at this slightly modified code

Code: [Select]
{1,2,3,4,5,6,7,8,9,0->L1
0->S                               
"1+10fPart((X+S)/10->Y1          //yay magic formula
                             
While 1

For(F,1,10                  //Display the entire linked list so you can see whats going on
Output(1,F,L1(Y1(F           //Displays elements from 1 to 10
End
rand(20

S+1->S                        //Increase shift
randInt(0,9->L1(Y1(10        //Add random number onto the 'end' of the list, which is element 10

End

I believe that does exactly what you are looking for.  Do you see how it works?  The numbers do wrap every time i change the shift, but then i overwrite them with the new random number that is being added onto the end of the list (End of the list is element 10)
Title: Re: Matrix Help
Post by: ztrumpet on January 30, 2010, 06:43:48 pm
That code is genius!
Would it be faster if you had Y1 hard coded in?
Title: Re: Matrix Help
Post by: cooliojazz on January 30, 2010, 08:54:17 pm
Yes I see it, but I have to have
[1,2,3]
[2,3,4]
not say,
[2,3,1]
[3,4,2]
Here's an explanation: XLib Tilemap :P
Title: Re: Matrix Help
Post by: Builderboy on January 30, 2010, 09:00:13 pm
Oh, i see.  Well then you might need to be a bit tricky.  Instead of using the tilemaper, you could do this.  When you first load up the screen, you use the tilemapper normally to fill the screen region.  Then when you shift the matrix using my matrix shifter, you use the xLib screen shift routine, and then use the tilemapper to add onto the end of the shifted screen tilemap.

Would that work?  As you can see I'm trying very hard to make this work without actually shifting the tiles :P

EDIT: Oh and zTrumpet,  it would be much faster, but i was coding for readability :)
Title: Re: Matrix Help
Post by: cooliojazz on January 30, 2010, 09:47:45 pm
Thats one of the possibilities I was considering, but I have a border around the outside and I kind've like it, so I was seeing if there was any fast way of just shifting the matrix, instead of having to redraw the border as well :P Plus, that would mean i would completely have to redo my collision and shifting part...
Title: Re: Matrix Help
Post by: Builderboy on January 30, 2010, 10:06:03 pm
Hmmm, well then i don't know :/ I guess you could always store the border into a pic and recall it...  

Well you might want to try rowswapping to get better speed, i think it would be faster than storing to lists each time, but i will have to run some tests...

Alright, instead of

Code: [Select]
[A]T->[A]
Matr->Lst([A],L1,L2,L3)
Lst->Matr(L2,L3,L1,[A])
[A]T->[A]

I think

Code: [Select]
[A]
For(F,1,2
rowSwap(Ans,F,F+1
End
Ans->[A]

Is a lot faster, and even faster the greater your matrix is.  Is 3x3 going to be the final size of your matrix?
Title: Re: Matrix Help
Post by: ztrumpet on January 30, 2010, 10:41:33 pm
Wow, an Xlib game that shifts like this!  That sounds really cool!

Builderboy, that's neat code too. :)
Title: Re: Matrix Help
Post by: cooliojazz on January 30, 2010, 11:15:48 pm
No, it'll be 6x10, and thanks for that, I think i'tll help :D
Title: Re: Matrix Help
Post by: nemo on May 15, 2010, 10:02:34 pm
using builderboy's method but transposing the matrix to deal with columns:
Code: [Select]
[A]T
For(F,1,2
rowSwap(Ans,F,F+1
End
AnsT->[A]
a capital T is denoting the transpose function since superscript doesn't work in a code block.

EDIT:
unrelated, needing 20 posts to talk in IRC is undesirable. Especially being a new member. Especially when someone says new members are "kicking in". So this is how i respond....
Title: Re: Matrix Help
Post by: DJ Omnimaga on May 15, 2010, 10:08:15 pm
Oh hi and welcome ^^

Btw, to use the upper T symbol, you can do this:

Code: [Select]
[A][sup]T[/sup]
It will display [A]T

Also the reason why we require 20 posts to use OmnomIRC is for spambot protection (and other spam attacks, like two weeks ago). You can access the channel even as guest or under 20 post from the Java client linked on top of the chat box, or by using an IRC client such as mIRC/X-Chat/Chatzilla, connecting on irc.efnet.org and joining #omnimaga
Title: Re: Matrix Help
Post by: nemo on May 15, 2010, 10:19:09 pm
ahh sorry, that makes more sense now lol.

as you can see by your post, superscript is not implementable in a code block.

I read the board/IRC rules. seems pretty straightforward except the double posting thing can get kind of icky. editing is so much simpler.
Title: Re: Matrix Help
Post by: DJ Omnimaga on May 15, 2010, 10:25:05 pm
Oh right I understand now. I guess it's because I am so used to use [ quote ] instead of [ code ] (thanks to SourceCoder2) since code messes up characters sometimes. All I be careful about is making sure to disable smileys before posting the code.

Btw, what I meant by "new members kicking in" is that the end-of-school-year rush started. Kicking in would mean they are arriving. It's typical for Omnimaga to get a considerable amount of new members from mid-May to the end of June.
Title: Re: Matrix Help
Post by: meishe91 on May 15, 2010, 10:35:39 pm
What does putting the T there do exactly?

Also ya, [code][/code] doesn't support BBCode but you can also do something like ^T and then say that is the little superscript T instead of the variable T.
Another quick, useful, BBCode command that is useful when talking about such things is the use of [nobbc][/nobbc] because anything in between them will not be made into the graphic equivalent when it is submitted. Just thought it'd be handy so people don't have to do [ quote ] and such :) (Sorry, I just enjoy using BBCode for some reason :P)
Title: Re: Matrix Help
Post by: DJ Omnimaga on May 15, 2010, 10:51:19 pm
lol I didn't even know  [nobbc] existed
Title: Re: Matrix Help
Post by: meishe91 on May 15, 2010, 11:08:44 pm
Haha ya, neither did I until I looked up the BBCode commands that SMF supported (there is a list on their site).
Title: Re: Matrix Help
Post by: ztrumpet on May 16, 2010, 06:23:43 pm
What does putting the T there do exactly?
T makes the Matrix flip along it's axis of (X,X).  Ex:
[[1, 2][3, 4] T is [[1, 3][2, 4]]
[[1, 2, 3][4, 5, 6]] T is [[1, 4][2, 5][3, 6]]
It's best used for manipulation matrices to use the row commands or Matrix_to_list if desired. :)