Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Known variables: Number of rows, number of columns, number of cells, cell index.

Find: column number relative to cell index.

Example:

1 2 3 4
5 6 7 8
9

What function can I use to find that 6 is in column 2 and row 2?

share|improve this question
    
6%4=2 So, number % the number of rows –  lab bhattacharjee Jan 31 '13 at 15:38
    
That's what I had in my code at first - not sure how I convinced myself it wasn't right. Maybe it's the lack of sleep; time for bed, thanks! –  Marc05 Jan 31 '13 at 15:50
    
Now that I'm awake, I remember why that didn't work - 8 mod 4 = 0. So any number on the 4th row will always = 0. I guess I'll have to do some conditionals to get it working, but I was hoping for some sort of expression or something. –  Marc05 Feb 1 '13 at 3:37

1 Answer 1

up vote 0 down vote accepted

For the row, you can use 1+floor((cell index-1)/number of columns)

For the column, 1+(cell index-1)% number of columns

The 1+ and -1 are reason to use a base of zero-then they go away.

Generally, if you have "end effects" like your 8%4=0 they can be handled by adding or subtracting to put the break where you want it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.