Thread: Get matrix indexes from the value

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58

    Get matrix indexes from the value

    Hello,

    I have a 3x3x3 matrix with values hanging from 1 to 27.
    The order is this:
    Code:
    m[0][0][0] = 1;
    m[0][0][1] = 2;
    m[0][0][2] = 3;
    m[0][1][0] = 4;
    m[0][1][1] = 5;
    m[0][1][2] = 6;
    .
    .
    .
    m[1][0][0] = 10;
    m[1][0][1] = 11;
    m[1][0][2] = 12;
    .
    .
    .
    m[2][2][0] = 25;
    m[2][2][1] = 26;
    m[2][2][2] = 27;
    Is there an easy way to compute the indexes given the value?
    For example, I know that the first index can be found by dividing the value by 9.0:
    Code:
    const int firstIndex = ceil(value / 9.0) - 1;
    What about the second and the third indexes? I couldn't find a pattern in the numbers, and I didn't want to iterate through the matrix (the indexes will be calculated many times).

    The other option is to create arrays where the values will be indexes and the matrix indexes will be the values. I'm not sure if it is the best way though, so I'd really appreciate any advice on this.

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Looking at the pattern, I'd say that for m[i][j][k] with value n:
    i = (n - 1) / 9
    j = ((n - 1) / 3) % 3
    k = (n - 1) % 3
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by laserlight View Post
    Looking at the pattern, I'd say that for m[i][j][k] with value n:
    i = (n - 1) / 9
    j = ((n - 1) / 3) % 3
    k = (n - 1) % 3
    Can that be said without even knowing m's type? I suppose they're ints but even then is padding an issue?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SirPrattlepod
    Can that be said without even knowing m's type? I suppose they're ints but even then is padding an issue?
    Well, if the values are of a floating point type, then yeah, there would be a problem with those expressions since they won't be using integer division. But if they are of an integer type, then there is no problem since this is just a matter of mapping one integer to another based on the mathematical pattern, i.e., the inner indices vary one by one, then the middle ones vary three by three, then the outer ones vary nine by nine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I guess I was confused by the strange question

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    indexes are just representation of number n-1 in notation with base 3 - so laserlight's formulas are just one of ways to convert the number to this notation...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    That's it, thank you laserlight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with Array indexes
    By workingOnMig in forum C Programming
    Replies: 8
    Last Post: 04-20-2011, 07:19 AM
  2. String indexes.
    By Crux in forum C++ Programming
    Replies: 1
    Last Post: 02-03-2011, 01:37 PM
  3. Getting Indexes
    By satty in forum C Programming
    Replies: 2
    Last Post: 08-09-2010, 01:11 AM
  4. Negative array indexes
    By jw232 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 08:32 PM
  5. MySQL Indexes
    By LuckY in forum Tech Board
    Replies: 1
    Last Post: 05-09-2007, 06:04 PM