Results 1 to 8 of 8
  1. #1
    tyang is offline Member
    Join Date
    Jan 2010
    Posts
    44
    Rep Power
    0

    Default How can I extract the last column of a given matrix?

    How can I extract the last column of a given matrix?
    For example,
    matrix[][] = new [10][4];

    How can I get the last column of a matrix as a vector?

    Thank you very much!:)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    By using a for loop and extracting each item, one at a time. If you experiment with it, you'll figure it out quickly.

  3. #3
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    I am a bit rusty... But I think this will do the trick:

    Java Code:
    Vector<whatEver> v = new Vector<whatEver>;
    whatEver matrix[][] = new whatEver[10][4];
    
    for(int i=0; i<matrix.length; i++){
         v.addElement(matrix[i][3]);
    }
    Who Cares... As Long As It Works...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by markw8500 View Post
    I am a bit rusty... But I think this will do the trick:

    Java Code:
    Vector<whatEver> v = new Vector<whatEver>;
    whatEver matrix[][] = new whatEver[10][4];
    
    for(int i=0; i<matrix.length; i++){
         v.addElement(matrix[i][3]);
    }
    Better to avoid magic numbers (i.e., the "3").

  5. #5
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Good point my friend...


    for(int i=0; i<matrix.length; i++){
    v.addElement(matrix[i][matrix[0].length-1]);
    }
    Who Cares... As Long As It Works...

  6. #6
    tyang is offline Member
    Join Date
    Jan 2010
    Posts
    44
    Rep Power
    0

    Default

    Hi Guys, Thank you so much!
    What does <whatEver> represent?

  7. #7
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    You have to specify what your vector is... AKA... String... Double... etc...
    Who Cares... As Long As It Works...

  8. #8
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    <whatEver> is the data type your matrix stores, for example, if your array stores ints, you would initialise you Vector as:
    Java Code:
    Vector<Integer> v = new Vector<Integer>(); //not Vector<int>, vectors hold objects, not primitives
    This is known as a generic, initialising the vector in this manner assures, that it holds, and accepts only Integer objects.

Similar Threads

  1. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 06:10 PM
  2. scalability of matrix
    By ajay kumar in forum New To Java
    Replies: 1
    Last Post: 12-07-2009, 08:21 AM
  3. Scalability of matrix
    By ajay kumar in forum New To Java
    Replies: 0
    Last Post: 12-04-2009, 05:19 AM
  4. [SOLVED] Help with Matrix
    By Bernard Robitaille in forum JCreator
    Replies: 10
    Last Post: 02-14-2009, 02:19 AM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •