### Re: How can I create a 2D array and methods to fetch/store

Posted:

**07 Feb 2017 07:45**Matrix classMethod: id( n )

| i m |

n n self new dup ->m

n loop: i [ m put( i, i, 1 ) ]

;

I sort of follow that this is a classMethod as opposed to a method. In it's definition it creates a new class of Matrix that's an identity matrix. The class of objects instantiates as an identity matrix. So a new class of matrix is created but I don't quite follow where SELF point's to. I guess I'm still fuzzy of the idea of SELF.

I'm patterning my algorithm off of id(n ) by putting values diagonally in the rows and columns in an pre-existing matrix. But I'm still unsure at going about it being that this was written for a classMethod and not a method.

Here's what I'm trying to do to a matrix object on the stack. Something like this:

\\ reading 4 right then 8 up. The 1 , 0 pair are the direction of the diagonal into a southeasterly quadrant.

<matrix> 4 8 1 0 diagonal .s

0 0 0 1 0 0 0 0

0 0 0 0 1 0 0 0

0 0 0 0 0 1 0 0

0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 1

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

I want to write this myself (I relish the challenge) but I'm a little unsure how to modify the id( n ) classMethod as a simple method as a starting point.

| i m |

n n self new dup ->m

n loop: i [ m put( i, i, 1 ) ]

;

I sort of follow that this is a classMethod as opposed to a method. In it's definition it creates a new class of Matrix that's an identity matrix. The class of objects instantiates as an identity matrix. So a new class of matrix is created but I don't quite follow where SELF point's to. I guess I'm still fuzzy of the idea of SELF.

I'm patterning my algorithm off of id(n ) by putting values diagonally in the rows and columns in an pre-existing matrix. But I'm still unsure at going about it being that this was written for a classMethod and not a method.

Here's what I'm trying to do to a matrix object on the stack. Something like this:

\\ reading 4 right then 8 up. The 1 , 0 pair are the direction of the diagonal into a southeasterly quadrant.

<matrix> 4 8 1 0 diagonal .s

0 0 0 1 0 0 0 0

0 0 0 0 1 0 0 0

0 0 0 0 0 1 0 0

0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 1

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

I want to write this myself (I relish the challenge) but I'm a little unsure how to modify the id( n ) classMethod as a simple method as a starting point.