Calculate what subgroup a number belongs to

I have a grid represented as follows...

01 02 03 04 05 06 07 08 09
10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81

I need a way to take any number between 1 and 81 and use that number as the basis for calculating what 'group' that number belongs to (groups named A..I). The groups are segmented as follows...

01 02 03          04 05 06          07 08 09
10 11 12          13 14 15          16 17 18
19 20 21          22 23 24          25 26 27

28 29 30          31 32 33          34 35 36
37 38 39          40 41 42          43 44 45
46 47 48          49 50 51          52 53 54

55 56 57          58 59 60          61 62 63
64 65 66          67 68 69          70 71 72
73 74 75          76 77 78          79 80 81

Once the group (for examples sake 'I') is known I need to determine the numbers that exist within its respective group. So for example...

#45 = GROUP F = 34,35,36,43,44,45,52,53,54

This can be achieved with a nasty switch statement I am sure, but I was hoping a more elegant solution might exist out there that uses some math formula?

Any ideas?
Blowfelt82Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TommySzalapskiCommented:
Say the rows and columns in the big grid are numbered from 0 to 8

floor((num-1)/9) gives the row (floor is rounding down)
(num-1) mod 9 gives the column (mod is remainder of division problem)

(row mod 3)*3 + (column mod 3) will give you 0 for A, 1 for B, 2 for C, etc
So (row mod 3)*3 + (column mod 3) + 'A' will give you the letter of the group
Or to expand it all out
group = (floor((num-1)/9) mod 3)*3 + (((num-1) mod 9) mod 3) + 'A'
0
aburrCommented:
You have a matrix so you can use some matrix ideas. Each of the original numbers can be written as
x sub i, j     where i is the row number and j is the row column of your number.

The groups can be labeled X sub I,  J where I and J go from 1 to 3.
Each of the nine groups are defined. ie
M sub 2,3 = {34, 35, 36, 43, 44, 45, 52, 53, 54}
now you just have to get I and J from the i and j of the number whose group you want.
I = i (cel 3)
J = j (cel 3)     where cel 3 means that you divide I by 3 and take the result with any non- zero integer part calling for the next highest integer.
Thus 45 is x sub 5,9 and the 5 = 2 and 9 = 3 so the 45 is in group 2,3 which is defined as the string for which you are looking.
0
Blowfelt82Author Commented:
Tommy - Thanks for the quick response/sorry for my late reply...

The row/column formulas work perfectly, but I cannot make the '(row mod 3)*3 + (column mod 3)' formula work. This is more likely to be my implementation than the formula! But when I use the formula with the number 44 for example I get... 4 = E (which is one group out), when I try with the number 12 I get 5 = F?

My test implementation in powershell is as follows...

$num = 12
$row = [System.Math]::Floor(($num-1)/9)
$col = ($num-1)%9
(($row%3)*3 + ($col%3))

Aburr: I will have a look into your solution and see what I get! Cheers.
0
TommySzalapskiCommented:
Oops, no. It was not your implementation, it was my mistake. It's /3 not %3 for that step.
$row = [System.Math]::Floor(($num-1)/9)
$col = ($num-1)%9
$group_num = [System.Math]::Floor($row/3)*3 + [System.Math]::Floor($col/3)
$group = [char](65 + group_num)

I don't know any powershell, but I think that last line should actually give you the letter. 65 is the ASCII value of 'A'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Blowfelt82Author Commented:
Worked like a charm thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.