storing a matrix

This is a discussion on storing a matrix within the C++ Programming forums, part of the General Programming Boards category; I want to make a program to do simple matrix calculations with the elements from a text file. that part ...

  1. #1
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20

    Question storing a matrix

    I want to make a program to do simple matrix calculations with the elements from a text file. that part is simple and i can handle. i want this program to be highly optimised in terms of memory etc when catering for huge matrices like 50X50 matrices perhaps. so what i thought about was to store the element value and its coordinates instead of storing the entire matrix which often contains many zeros.

    so for example if i had the matrix
    2 0 6
    0 1 0
    0 0 5

    what i would really store here is (2, 0, 0) which represents the element 2, stored in row 0, column 0. and so on for the rest. so what i need help with is finding the best way to store that data perhaps using struc. also i dont know much about struc or classes. i am reading the msdn but still confused.

    any ideas?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    any ideas?
    Yes, don't worry about it
    A 50X50 has 2500 elements. If they're ints thats a mere 10 K of memory. In some platforms this is bad, but for most all it is not.

    Anyhow, a vector of vectors or just a static (or dynamic) 2D array would work.

    what i would really store here is (2, 0, 0) which represents the element 2, stored in row 0, column 0.
    What is the point of that?
    array[0][0] = 2 gives you those 3 pieces of information without actually storing but 1. For speed, you could make the array 1D given you know the width

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    classes and structs are the same, the only difference being that the default for a class is private and the default for a struct is public. people sometimes tend to use a struct when just storing data, but you might as well just use a class for whatever you need. in your case I'd have a class that stores the data in a 2 dimensional array, and have a few functions for adding or updating the values at any given coordinate:
    Code:
    // function prototype
    void changeElement(int value, int col, int row);
    
    // call to function
    matrix.changeElement(2, 0, 0);
    that would be a function to change the element at pos 0, 0 to the value 2. the way you wanted it. if you know how to write functions that should be easy to do. i'd also have a couple functions, one for returning the value in the element at any given coordinate:
    Code:
    // function prototype
    int get(int col, int row);
    
    // call to function
    matrix.get(0, 0);
    and there could be a function just to print out the whole matrix. if you needed it... i don't know about matrices 50x50 or bigger though. sounds like you just need to store data more than print it all out at once.

    anyway read about classes, they're very helpful, you'll be allowed to make your own matrix class so that you just define a variable of the matrix type and use the built in functions to store and retrieve data. it's not hard

    and yea, what madcow257 said about memory. if a 50x50 matrix of integers to you is huge, then don't worry about memory
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  4. #4
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    What is the point of that?
    array[0][0] = 2 gives you those 3 pieces of information without actually storing but 1. For speed, you could make the array 1D given you know the width
    yea i know what you mean. it does make no sense to store 3 bits of info to represent 1 value. i tried it with a vector and it worked good. thanks man.

    i am trying to expand my abilities with using structures and classes though, thats why i want to try the same problem but without the vector idea.

    ok while i was typing this, i got another reply so i am going to try what linucksrox suggested. linucksrox thank you too. i am about to try your suggestion. reading about classes as we speak, or type
    Last edited by cdkiller; 03-14-2006 at 07:02 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It is true that you shouldn't worry about optimizing away part of 2500 values, but if you want to do it as an exercise you could use a map. Make the key of the map the coordinates in the matrix, and the value is any non-zero value that shows up in the matrix. For small matrices (like yours) it won't save any memory because of the overhead of the map, but for very large matrices with only a few non-zero values it will save memory because, as you said, you are only saving information about those non-zero values.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    u could use maps? sounds interesting.... where can i learn how to use functions that does stuff like that? i tried lookin in msdn and didn't really find anything
    if u store these vectors like coordinates... how would u go about doing arithmetic functions on these two vectors?

  7. #7
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    predude
    Error, name too close to senior membor -O

    u could use maps? sounds interesting.... where can i learn how to use functions that does stuff like that?
    http://www.codeproject.com/vcpp/stl/stlintroduction.asp

  8. #8
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    i was reading about classes etc however i used the idea of the array to store the values. then another problem came to me, if i read 1 matrix from a text file everything is fine, when i try reading another matrix from the same file i get problems because i cant detect where the empty line is in the text file. so its like this

    2 3
    5 1

    6 1
    4 9


    the problem is that it reads the whole dam thing as 1 matrix. what i was using to read the data in was getline, and then i used a split function to get the individual numbers, then i used atoi to convert them to integers then i stored them to a vector.

    so how do i detect the empty line and make my code place that new section to a new vector. i tried conditional statements to look for the newline character \n but a slew or errors told me that my idea could not work, lol. evidently i did not do it correctly, any ideas people? btw the condition i used was if (x != '\n'), i also tried if (x = '\n'). where x is the string that i got using getline. im not sure but i think the errors were telling me that i could not check if a string was equal to a char. like i said, im not too sure so i need some help please.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    If you call getline and the line is empty, then x will be empty and x.empty() will return true (assuming a C++ string).

  10. #10
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    ahh yes x.empty(). tried it and it works. thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. Replies: 3
    Last Post: 02-26-2008, 01:12 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM

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