Community Server

The platform that enables you to build rich, interactive communities
Welcome to Community Server Sign in | Join | Help
in Search

How to TAKE an element of a matrix (2x2)?

Last post 05-26-2007, 9:09 AM by Random Walker. 3 replies.
Sort Posts: Previous Next
  •  05-23-2007, 2:13 PM 32

    How to TAKE an element of a matrix (2x2)?

     I have a 50x50 matrix of data. I would like to be able to TAKE or grab data in different cells of the matrix  like in other programming languages like, for example, in  x=M(i,j). Is there some way of doing this in Statitistics101?    

    One idea is to change the matrix into a single vector but not quite sure how to do it so its elements are accessible.

     

    Thnanks

     

     

     

     

     

     

  •  05-24-2007, 7:50 AM 33 in reply to 32

    Re: How to TAKE an element of a matrix (2x2)?

    If you can get the matrix into a vector, then it's not too
    hard to pull out an individual element based on the row and
    column numbers using a subroutine.

    Say you have the matrix:

    1 2 3
    4 5 6
    7 8 9

    and you put it into a vector, row by row, so that the vector
    looks like this:

    COPY (1 2 3 4 5 6 7 8 9) vec

    Then you can use this subroutine to pull out a copy of the
    element at vec(row, col). If you're numbering the rows and
    columns from zero, such that the first row is row zero and
    the first column is column zero, then the subroutine is:

    'vec contains a matrix in row by row order.
    'First element is (0,0).
    NEWCMD GET vec rowSize row col element
       ADD col 1 newCol
       MULTIPLY row rowsize rowStart
       ADD rowStart newCol elementPosition
       TAKE vec elementPosition element
    END


    where rowsize is the number of elements in a row
    (which is 3 in this example). Here's a couple of
    lines to select element (2,2):

    GET vec 3 2 2 element
    PRINT element


    The result is:

    element: 9.0

    Here's a program that loops over the vector's elements and prints them out:

    'Here's an example program that uses the subroutine in
    'a pair of nested loops so it can print out the elements
    'one by one. This assumes that the subroutine is
    'the one where the first element is (0,0).
    COPY 0 row
    REPEAT 3
       COPY 0 col
       REPEAT 3
          GET vec 3 row col element
          PRINT element
          ADD 1 col col
       END
       ADD 1 row row
    END


    If you want to have the first row and column be numbered one
    instead of zero, then you have to change the subroutine to this:

    'vec contains a matrix in row by row order.
    'First element is (1,1).
    NEWCMD GET vec rowSize row col element
       SUBTRACT row 1 newRow
       MULTIPLY newRow rowsize rowStart
       ADD rowStart Col elementPosition
       TAKE vec elementPosition element
    END


    The same element selection as above:
    GET vec 3 2 2 element
    PRINT element


    produces:

    element: 5.0

    Alternatively, if you have stuffed your matrix into the vector column by column, like
    this:

    COPY (1 4 7 2 5 8 3 6 9) vec

    Then the subroutines change to these:

    'vec contains a matrix in column by column order.
    'First element is (0,0).
    NEWCMD GET vec colsize row col element
       ADD row 1 newRow
       MULTIPLY col colsize colStart
       ADD colStart newRow elementPosition
       TAKE vec elementPosition element
    END


    'vec contains a matrix in column by column order.
    'First element is (1,1).
    NEWCMD GET vec colsize row col element
       SUBTRACT col 1 newCol
       MULTIPLY newCol colsize colStart
       ADD colStart row elementPosition
       TAKE vec elementPosition element
    END


    If this doesn't help, let me know more about your problem. Maybe there's  another way to solve it.
  •  05-26-2007, 7:43 AM 34 in reply to 33

    Re: How to TAKE an element of a matrix (2x2)?

    That did the trick. Thanks again. Are you the developer of Statistics101? Might send a donation.

     

     

     

  •  05-26-2007, 9:09 AM 35 in reply to 34

    Re: How to TAKE an element of a matrix (2x2)?

    Yes, I'm the developer.
    Glad to be of help.

    John
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems