how to find common elements between different matrices

I have 3 matrices. I want to find the shared elements within these matrices with the number of repetition.
matrix 1:
1 2 3 4
5 3 2 1
3 2 1 0
0 2 1 4

matrix 2:
1 2 3 4
0 1 2 1
3 1 0 2
2 5 3 1

matrix 3:
2 3 1 0
3 2 1 0
4 0 2 1
2 3 0 1

imagine these three matrices make the Local_Connectivities which is a vector of vector of vector and each of these matrices are going to be sent to different processors. before that I need to know which points will be shared between processors.

the thing that come to my mind is looping over all matrices , but I think it gonna be a little messy, having many for loops with if condition.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (int i_proc = 0; i < num_procs; i_proc++) {
	for (int j = 0; j < Local_Connectivities[i_proc].size(); j++) {
		for (int k = 0; k < Local_Connectivities[i_proc][j].size(); k++) {
			for (int i = 0; i < num_procs; i++) {
				for (int jj = 0; j < Local_Connectivities[i_proc].size(); jj++) {
					for (int kk = 0; k < Local_Connectivities[i_proc][j].size(); kk++) {

						if (Local_Connectivities[i_proc][j][k] == Local_Connectivities[i][jj][kk])
						{

							VecIdx_t shared;
							shared.push_back(Local_Connectivities[i_proc][j][k]);

						}
					}
				}
			}				   
		}
	}
}

Last edited on
you are making more work for yourself.
if you need to know something like this, track it on the front end... when you divide up the mesh to do your parallel processing, either do it in a systematic way (eg, every 1000x1000 block) or if you need to do it irregularly, store something -- if they are rectangular store the top and bottom corners (diagonal, top left bottom right). If they are not rectangular, you may need to store the coordinates of the entire boundary of each ROI. But don't try to *find* something like this, you already KNOW it, somewhere, somehow, so take what you know and stow it rather than try to rebuild it.
If this is not sufficient to detect overlapped points, it is enough to re-create that with minimal effort I believe.
Last edited on
I changed my question considering the possible solutions have been pointed out. Anyhelp would be appreciated.
Last edited on
Please don't edit your question in the future. It makes the response meaningless to those who read the thread later.

Instead, post the updated question in a subsequent reply.

Similarly, don't edit posted code with updates. This will make comments on the original code completely nonsensical.

Edit earlier posts to correct spelling or similar, but never edit a post to reflect changes made after responses have be provided.

Thanks.
what is a point, in the above?
matrix 1,2,3 all[0] ?

I think you need a concept of a point in your code. a tuple or something simple will do.
then a map or set or something to contain them will let you quickly see if a is in b etc.
Last edited on
Topic archived. No new replies allowed.