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

• 02-06-2010, 03:25 AM
tyang
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!:)
• 02-06-2010, 03:47 AM
Fubarable
By using a for loop and extracting each item, one at a time. If you experiment with it, you'll figure it out quickly.
• 02-06-2010, 03:48 AM
markw8500
I am a bit rusty... But I think this will do the trick:

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]); }```
• 02-06-2010, 03:50 AM
Fubarable
Quote:

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

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").
• 02-06-2010, 03:55 AM
markw8500
Good point my friend...

Quote:

for(int i=0; i<matrix.length; i++){
}
• 02-06-2010, 06:43 AM
tyang
Hi Guys, Thank you so much!
What does <whatEver> represent?
• 02-06-2010, 06:47 AM
markw8500
You have to specify what your vector is... AKA... String... Double... etc...
• 02-06-2010, 06:48 AM
m00nchile
<whatEver> is the data type your matrix stores, for example, if your array stores ints, you would initialise you Vector as:
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.