Like Tree1Likes
  • 1 Post By laserlight

Get matrix indexes from the value

This is a discussion on Get matrix indexes from the value within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a 3x3x3 matrix with values hanging from 1 to 27. The order is this: Code: m[0][0][0] = ...

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

    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
    20,976
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,976
    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.
    SirPrattlepod likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    52
    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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21