How to make a matrix AxB using CRPL?

Started by pawel345, January 29, 2014, 09:23:19 AM

Previous topic - Next topic

pawel345

I know it's possible but how to do it? I know how to make a List, but how to change a list to a matrix? For example I would like to have a matrix Map Width by Map Height. The concept somehow eludes me. If someone could write a function to handle something like that I would be very thankful.

Grayzzur

#1
Basically you just have a bigger list and do the math yourself. That's all an array is behind the scenes.

If you want an array that's 10x10, you make a list of 100, and access element (x+y*10).

Edit: In case it's not clear which 10 to multiply Y by --
Array [Width, Height] would be List [Width*Height] and access element [x,y] in the array would be [x+y*Width] in the list.
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

eduran

I've been using this function:
CreateListStartingSize(MapWidth mul(MapHeight)) ->2DArray
Set/GetListElement(<-2DArray @Coord2Index(<-x <-y))

:Coord2Index
# takes cell coordinates x and y and transforms them into the corresponding array index
# allows usage of lists (=1D arrays) as if they were two dimensional
# x1 y1 - i1
swap MapHeight mul add



pawel345

thanks :D I knew it must have been something easy that I just couldn't get :D

Clean0nion

Other than almost all the topics in Builder's Corner, there so far have been no topics of which to me the concept has made no sense - this is the first.

knucracker

Here is a 2x2 array.
0 1 2
3 4 5
6 7 8

Note the index of each position.  That is the same as this single dimensional list:
0 1 2 3 4 5 6 7 8

You can represent any n dimensional array with a single dimensional list, so long as you define the extents of each dimension beyond the first dimension ( you must define the the width for instance in a 2 dimensional array).

Michionlion

#6
Here's how I think of it, C0.  You basically have a 2D array, right?  However, instead of using colums like this:


1,1 | 2,1 | 3,1
1,2 | 2,2 | 3,2
1,3 | 2,3 | 3,3


We only designate one index for each coord (that way it fits in a single list), and it loops around, like this:


0 | 1 | 2
3 | 4 | 5
6 | 7 | 8


Therefore, we can access each 'coordinate' from x,y coordinates by adding 3 for each y increment - for instance, if you wanted to get index 5, which corresponds to 2,1, you take the x coord and then add the y coord multiplied by the width of the array (3), like so:


x + (y*width)
2 + (1*3)


Note that the array MUST be zero based for this method to work. (if not zero-based, you must add the base amount (so if you started indexing at 1, you would add 1) to the output of the above formula - so it would become base+x+(y*width).  You cannot do this - at least, not easily - with a non-zero based coordinate system without first converting to zeroed. I hope that explains it!

EDIT:  and I spent all that time making this just to have V respond before me.... :P
"Remember kids, the only difference between science and messing around is writing it down."
                                                                                                                         - Adam Savage

My website
My CW1, and CW2 maps!

Clean0nion

Right - thanks. I'd just use two lists, but I'm not sure if that would achieve the same effect.

EDIT: That is thanks to both of you, of course.

Michionlion

It would achieve the same effect, but with more complication than necessary, although it would be easier to understand.
"Remember kids, the only difference between science and messing around is writing it down."
                                                                                                                         - Adam Savage

My website
My CW1, and CW2 maps!

Grauniad

Quote from: Clean0nion on January 29, 2014, 11:30:08 AM
Right - thanks. I'd just use two lists, but I'm not sure if that would achieve the same effect.

EDIT: That is thanks to both of you, of course.
How would you use two lists to represent a 64 x 64 array?
A goodnight to all and to all a good night - Goodnight Moon

Clean0nion

#10
Quote from: Michionlion on January 29, 2014, 11:33:43 AM
It would achieve the same effect, but with more complication than necessary, although it would be easier to understand.
Which reminds me - and I'll stop posting after this post - one of my projects has about 16 lists in it, 12 of which are used simultaneously, and all are per script per unit. Though in some units only one of the lists is used.

@Grauniad

64 0 do
  64 0 do
    <-x J AppendToList
    <-y I AppendToList
  loop
loop

something like that

Michionlion

Quote from: Grauniad on January 29, 2014, 11:36:29 AM
Quote from: Clean0nion on January 29, 2014, 11:30:08 AM
Right - thanks. I'd just use two lists, but I'm not sure if that would achieve the same effect.

EDIT: That is thanks to both of you, of course.
How would you use two lists to represent a 64 x 64 array?

hmm... you're right!  You'd have to do a list inside a list, and I don't know if that is possible.
"Remember kids, the only difference between science and messing around is writing it down."
                                                                                                                         - Adam Savage

My website
My CW1, and CW2 maps!

Clean0nion

Quote from: Michionlion on January 29, 2014, 11:41:20 AM
Quote from: Grauniad on January 29, 2014, 11:36:29 AM
Quote from: Clean0nion on January 29, 2014, 11:30:08 AM
Right - thanks. I'd just use two lists, but I'm not sure if that would achieve the same effect.

EDIT: That is thanks to both of you, of course.
How would you use two lists to represent a 64 x 64 array?

hmm... you're right!  You'd have to do a list inside a list, and I don't know if that is possible.
Yes I lied that I wouldn't post anymore, but you should understand that I have no idea what I'm taking about.

Actually, you could do a list inside a list now that I think about it...
using <-! and ->!

eduran

Quote from: Michionlion on January 29, 2014, 11:41:20 AM
hmm... you're right!  You'd have to do a list inside a list, and I don't know if that is possible.
You can do lists inside lists, but to store a 64x64 matrix you'd need 9 size-8 lists in total.

Karsten75

Quote from: Clean0nion on January 29, 2014, 11:43:39 AM
I have no idea what I'm taking about.

word.

Quote from: eduran on January 29, 2014, 11:44:28 AM
Quote from: Michionlion on January 29, 2014, 11:41:20 AM
hmm... you're right!  You'd have to do a list inside a list, and I don't know if that is possible.
You can do lists inside lists, but to store a 64x64 matrix you'd need 9 size-8 lists in total.

I'dthink, in my simplicity, that you'd have to have more than that...