2D array to include multiple elements per index

I need a 2d matrix that points to 2 values from every row and column index. For every mat(index x,index y) I want to access age and waist circumference. I considered designing the solution with an array of arrays but I wasn't sure it was the right fit. Consider this code:
1
2
Mat A(400,400); //160000 elements
    A(15,315)--(accesses)-->29, 37 (for example).

Please advise how to approach this with attention paid to speed.
Last edited on
Class types can be used to define "bundles" of multiple data objects.

You could define, for example, an array of records:
struct record { int age, waist_circumference; };
Try any beginner's tutorial on structures or classes.

For instance the one here:
https://cplusplus.com/doc/tutorial/classes/
Thanks I looked at it and learned at least how I could use the data objects to solve the problem. I see now that the data objects could contain both the ages and circumferences. But how do I make 400 x 400 = 160000 data objects? How do I index the objects (400,400)? Do I need anything different than OOP if I were using the OpenCV environment for this exercise?
Last edited on
Do I need anything different than OOP if I were using the OpenCV environment for this exercise?

I don't know anything about OpenCV.

But how do I make 400 x 400 = 160000 data objects

Declare an array.
record a[400][400];

I don't know anything about OpenCV

My apologies for presuming.

Regarding the OpenCV question, I'm just looking at the fastest way to finish this problem. This will give me a good start.
Where is your data coming from - and in what format? What questions are you being asked to answer from the data?
I'm just looking at the fastest way to finish this problem. This will give me a good start.


this honestly looks like homework given the small amount of data you are looking at. The fastest answer isn't worth it, you will be fine with a decent answer.

The fastest way depends on details. Just a few of the more basic questions would be..
-how much data do you expect? (you answered this one, its not much).
-how do you need to access it most often? (by index at random?)
-is it very volatile (tons of adds and deletes) or nearly static (load and rarely change)?
- do you need to keep it in any kind of order (say by age?)

To me this looks like a 1-d problem. why do you have 2 indices into your data, what are those, and can you knock that down to ONE??
consider:
struct
{
int age; //could also be a birth date
double waist;
};
vector<age> alldappl(160000); //its ok if the size is too small, it can grow later,
//but give it a ballpark best guess

cout << alldappl[index].age; //but what is index ? is it part of the data like their SSN?
if index is from the data then an unordered map<> might be better. If its 0-159999 then this is fine. 400x400 can be directly converted into 0-159999 or you can use a 2d array if you want.
Last edited on
Building on the posts so far, is there a simple change to the following code that would allow the program to store a second element in arr index arr[0][2]?

1
2
3
4
float arr[5][5];
	arr[0][2] = 100; // 100 and 25 for example
	std::cout << arr[0][2] << std::endl;
        


If not, will create a solution with an array of objects or something like that.

Last edited on
a float array stores one float at that location.
there are built in containers, like tuple and pair, but those use unfriendly names in exchange for not making your own class or struct, sometimes a good tradeoff and sometimes not depending on how much you invoke the field names.

you can do screwy stuff, not recommended, like make the data an array of bytes that you stuff with whatever. This is almost exactly the same end result as the above, except now your code would be full of gibberish and look like a C programmer got ahold of it at 3am.

you can make the array hold only pointers back to the real data, and sometimes there are merits to that to make the array more performant** or easier to rearrange, but without a good reason, this is like the above, a layer of gibberish for no really good reason.

** in some use cases. The extra layer costs you, so you need to make up for that somehow to justify it, on TOP of the complexity increase in the code base.

in case you did not know, a solid block 2d array can collapse to 1d on demand. A split block cannot. things allocated like type name[rows][cols] are a solid block. Allocating with vector of vectors or double pointers are not solid. Solid blocks are your friend. They help with page faults and other aggravations.
Last edited on
to store a second element in arr index arr[0][2]


Sure,
float arr[5][5][2];
arr[0][2][0] = 100;
arr[0][2][1] = 25;
std::cout << arr[0][2][0] << ", " <<arr[0][2][1] << std::endl;



What is the significance of OpenCV in this thread, BTW?
It stores things in a cv::Mat or something. That's a completely different array structure.
Last edited on
What is the significance of OpenCV in this thread, BTW?
It stores things in a cv::Mat or something. That's a completely different array structure.


Honestly I wanted to see how the matrix generation and access would happen in this environment versus the OpenCV one. Matrices are probably optimized by virtue of their native programming in OpenCV, evaluating it might be useful.

float arr[5][5][2];
arr[0][2][0] = 100;
arr[0][2][1] = 25;
std::cout << arr[0][2][0] << ", " <<arr[0][2][1] << std::endl;


This answer (which works) by having the third dimension seems like OOP which uses the "." instead.

Thanks!

things allocated like type name[rows][cols] are a solid block.


great advice!
Last edited on
Topic archived. No new replies allowed.