is it possible to....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • greenflame

    is it possible to....

    Is it possible to swap two elements of an array? for example like swap
    rows in the matrix:

    [1 5 7 3]
    [2 6 2 5]
    [6 5 1 7]

    where I make a matrix as an array of arrays. where the inner arrays are
    the rows of the matrix. so the above could be coded as:

    A = [
    [1,5,7,3],
    [2,6,2,6],
    [6,5,1,7]
    ];

  • RobG

    #2
    Re: is it possible to....

    greenflame wrote:[color=blue]
    > Is it possible to swap two elements of an array? for example like swap
    > rows in the matrix:
    >
    > [1 5 7 3]
    > [2 6 2 5]
    > [6 5 1 7]
    >
    > where I make a matrix as an array of arrays. where the inner arrays are
    > the rows of the matrix. so the above could be coded as:
    >
    > A = [
    > [1,5,7,3],
    > [2,6,2,6],
    > [6,5,1,7]
    > ];
    >[/color]

    Below is some commented code with alerts to show what is happening
    - you can safely remove the alerts.

    You may want to check that A, A[r1] and A[r2] actually exist before
    trying to swap them.


    function swapRows( A, r1, r2 ){
    var i = A.length;
    alert(A.join('\ n'));
    A[i] = A[r1]; // Copy r1 to an extra element
    alert(A.join('\ n'));
    A[r1] = A[r2]; // Copy r2 into r1
    alert(A.join('\ n'));
    A[r2] = A[i]; // Put remembered r1 into r2
    alert(A.join('\ n'));
    delete A[i]; // Remove copy of r1
    alert(A.join('\ n'));
    }

    B = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
    ];

    swapRows( B, 0, 2 );


    --
    Rob

    Comment

    • RobG

      #3
      Re: is it possible to....

      RobG wrote:
      [...][color=blue]
      > function swapRows( A, r1, r2 ){
      > var i = A.length;
      > alert(A.join('\ n'));
      > A[i] = A[r1]; // Copy r1 to an extra element
      > alert(A.join('\ n'));
      > A[r1] = A[r2]; // Copy r2 into r1
      > alert(A.join('\ n'));
      > A[r2] = A[i]; // Put remembered r1 into r2
      > alert(A.join('\ n'));
      > delete A[i]; // Remove copy of r1
      > alert(A.join('\ n'));[/color]

      Forgot to mention that as A is just a reference to B, whatever we do
      to A also happens to B, so:

      alert(B.join('\ n'));

      shows that B now has its rows swapped.

      [...]


      --
      Rob

      Comment

      • Jc

        #4
        Re: is it possible to....

        greenflame wrote:[color=blue]
        > Is it possible to swap two elements of an array?[/color]

        Sure, array's are read/write, so just use a temporary var while
        swapping so you don't lose one of the array items.

        Example:

        <script>
        var a1 = [1,2,3];
        var a2 = [4,5,6];
        var aMatrix = [a1, a2];
        alert("before swap:\n"+aMatri x.join("\n"));

        var aTemp = aMatrix[0];
        aMatrix[0] = aMatrix[1];
        aMatrix[1] = aTemp;
        alert("after swap:\n"+aMatri x.join("\n"));
        </script>

        Comment

        • Lee

          #5
          Re: is it possible to....

          greenflame said:[color=blue]
          >
          >Is it possible to swap two elements of an array? for example like swap
          >rows in the matrix:
          >
          >[1 5 7 3]
          >[2 6 2 5]
          >[6 5 1 7]
          >
          >where I make a matrix as an array of arrays. where the inner arrays are
          >the rows of the matrix. so the above could be coded as:
          >
          >A = [
          >[1,5,7,3],
          >[2,6,2,6],
          >[6,5,1,7]
          >];[/color]

          Certainly.
          But I can't be more specific without more than a "for example".

          Comment

          • greenflame

            #6
            Re: is it possible to....

            I had a problem with the part where you remove the last row. It didnt
            remove it, well yes but no. Anyway I fixed it by using the pop command.
            Thanks!

            Comment

            Working...