|
Author |
Topic: doing transforms and inverses on a matrix (Read 996 times) |
|
sspboyd
|
doing transforms and inverses on a matrix
« on: May 24th, 2004, 10:59pm » |
|
Hello, I am trying to figure out how to 'rotate' or 'flip' a 5X3 matrix. Does anyone have an example they could share or point me towards some helpful urls? Thanks, steve eg Code: 1| 2| 3| 4| 5 ____________ 6| 7| 8| 9|10 ____________ 11|12|13|14|15 |
| rotate to Code: 11| 6| 1 -------- 12| 7| 2 -------- 13| 8| 3 -------- 14| 9| 4 -------- 15|10| 5 |
| thanks steve
|
gmail.com w/ sspboyd username
|
|
|
TomC
|
Re: doing transforms and inverses on a matrix
« Reply #2 on: May 24th, 2004, 11:32pm » |
|
I think it's maths Mikkel Answer... depends how they're stored. Naively, given: Code: int matrix[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; |
| The rotated version you request is... Code: int rotated[] = { matrix[10], matrix[5], matrix[0], matrix[11], matrix[6], matrix[1], matrix[12], matrix[7], matrix[2], matrix[13], matrix[8], matrix[3], matrix[14], matrix[9], matrix[4] }; |
| Which you can use like this... Code: int matrix[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; void setup() { println("matrix:"); for (int i = 0; i < matrix.length; i+=5) { println(matrix[i] + ", " + matrix[i+1] + ", " + matrix[i+2] + ", " + matrix[i+3] + ", " + matrix[i+4]); } int rotated[] = { matrix[10], matrix[5], matrix[0], matrix[11], matrix[6], matrix[1], matrix[12], matrix[7], matrix[2], matrix[13], matrix[8], matrix[3], matrix[14], matrix[9], matrix[4] }; println("rotated:"); for (int i = 0; i < rotated.length; i+=3) { println(rotated[i] + ", " + rotated[i+1] + ", " + rotated[i+2]); } } |
|
|
|
|
|
sspboyd
|
Re: doing transforms and inverses on a matrix
« Reply #4 on: May 25th, 2004, 12:58am » |
|
excellent. thanks (again) tom. Is there a way to do this algorithmically so that I can use random sizes of matrices? Or would this require using Jama? http://math.nist.gov/javanumerics/jama/ I was getting to the point of considering outputting an image of grey rects where the float vals in the matrix corresponded to the grey level, then flip/rotating or whatever the image and reading out the colour vals again into a new matrix. I am also thinking about using a modified version of mKoser's suggestion too. Thanks for the quick response. steve
|
gmail.com w/ sspboyd username
|
|
|
TomC
|
Re: doing transforms and inverses on a matrix
« Reply #5 on: May 25th, 2004, 1:16am » |
|
I think if you have a 2D array of this form: Code: int matrix[][] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 } }; |
| Then this is a general function that will flip it like you want: Code: int[][] rotateMatrix(int[][]given) { int rotated[][] = new int[given[0].length][given.length]; for (int i = 0; i < given[0].length; i++) { for (int j = 0; j < given.length; j++) { rotated[i][j] = given[given.length-1-j][i]; } } return rotated; } |
| It assumes there is at least one row, and that all rows are the same length. You could write similar code to do a 1D array, if you know the width and height. If you're doing it to pixels (images), it's probably more flexible to use transforms (translate, rotate, scale, etc).
|
« Last Edit: May 25th, 2004, 1:18am by TomC » |
|
|
|
|
sspboyd
|
Re: doing transforms and inverses on a matrix
« Reply #6 on: May 25th, 2004, 10:23am » |
|
Quote: If you're doing it to pixels (images), it's probably more flexible to use transforms (translate, rotate, scale, etc). |
| Im actually doing it to a probability matrix that does eventually end up as pixels but I don't want to copy the pixels from the results of one matrix and use them again (or else it wouldn't be a probability matrix). Thanks for your help. I will let you know how the program turns out when I get time to finish it. steve
|
gmail.com w/ sspboyd username
|
|
|
|