3

After reading 10+ threads about converting an entire array from 2D to 1D and vice versa, I'm wondering if there is a mathematical formula, requiring no iteration to return the index position of a pair of integers for a sorted 2D array.

My "grid" is always square, can be any size (it's 3x3 in this example), and I've got a sorted block of human friendly values from which I need to retrieve what I'm calling "True 1D indices" like this:

"Human-friendly" coordinates| Java 2D Indices    |  True 1D indices
    [1,1],[1,2],[1,3],  ==>   [0,0],[0,1],[0,2], ==>  0 , 1 , 2
    [2,1],[2,2],[2,3],  ==>   [1,0],[1,1],[1,2], ==>  3 , 4 , 5
    [3,1],[3,2],[3,3],  ==>   [2,0],[2,1],[2,2], ==>  6 , 7 , 8

So I need a method for my class to give me the following results:

I enter 1,1 and get back 0, I enter 3,2 and get back 7, I enter 2,3 and get back 5, etc etc...

I've played around with half a dozen equations where I try things like adding the coordinates to the previous row index squared, and I can never get the right result for every cell in the grid. Is there some special operator or Math function I'm missing?

Thanks.

2 Answers 2

2

let the matrix be of 3*3 ,3 rows and 3 column and we have to find the indices of (i,j) then the formula will be. indices= 3*(i-1)+j;

Note :- here 3*3 and i,j are not it java 2d array format it is in human friendly coordinates.

Example (i,j)=(2,3) indices=3*(2-1)+3 =6 And since your indices starts from 0 you could simply subtract one from 6 i.e. 5.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you got it. So the first constant is the number of total rows in human friendly form. Subtracting one at the end and in the parens is essentially the same as using the java row, column values. So when I test (N-rows* Java-row)+Java_column, I get the right answer for all 9 cells. May I ask how you knew this? Is this just a common formula for array flattening? does it have a proper name? Thanks so much.
No, i just try to individually frame different formulas for different values of the array and at the last came upon with this formula.I don't know if this formula has a proper name.
0

If the indices are named [index1,index2],

1DIndex = (rowNumber*index1) + index2

Where rowNumber starts at 1 for the first row, and index1 and index2 are the Java indices.

4 Comments

If I enter 3,2 and want to get back 7, how would that look in your example?
Your java indices are 1 less than the input indices. So your method would first decrease them both by 1 to 2,1. Then, 2,1 is in the third row of a 3x3 matrix, so 3*2+1=7.
So you would also need to know the size of the grid in order to know the row number.
For the second row, when I enter 2,1 your method says I should do: (2 * 1) + 0 ...this equals '2', but the number in position 2,1 is actually '3'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.