MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Question about ordering and sorting matrices

Asked by Brian on 5 Dec 2012

Hello, thanks for looking at this,

I have a peculiar matrix that I would like to order and sort. The matrix itself is imported form a geometry file exported from Ansys, and it looks like:

``` 18f 1b6 368 1 db3
18f 252 1b6 1 e60
1b6 252 368 1 da7
18f 368 252 1 e3e
53 194 3e 2 1d0
53 29c 194 2 1100
194 29c 3e 2 884
53 3e 29c 2 71d
26f 2fa 2d1 3 426
26f 2ba 2fa 3 1b0
2fa 2ba 2d1 3 847
26f 2d1 2ba 3 10fd
34f 1e7 27b 4 925
34f 31d 1e7 4 5bc
1e7 31d 27b 4 92c
34f 27b 31d 4 2a8
222 21e 2b3 5 4ce
222 3a7 21e 5 1127```

Now, here is a bit of information on this matrix. The first three column entries are hexadecimal point indices, and they form a triangle. Meaning, in the first row: point 18f is connected to 1b6 and is connected to 368 (all hex), and this forms a triangle.

The last two column entries are tetrahedra indexes, and are important in the fact that for each tetrahedra, there are at least 3 rows. The reason there are at least three is there are four triangles for each tetrahedra, however, some of the triangles are surface elements (which I don't need). There are at least three of each tetrahedra index, but the cumulative 3 indexes exist in the thid and forth column, index 1 is special because all four indexes exist in column four. This doesn't always happen, this is especially evident as you progress through the face matrix, since Ansys doesn't want to export duplicate entries.

Now, what I think I can do in order to have a single tetradedra index column that is ordered by 1 to N, and has at least 3 of each index (i.e. 1 1 1, 2 2 2, etc) is concatanate the fourth and fifth columns, as that would make a single column where every tetrahedra index is listed at least three times, and then I can check the first three columns and see if any of the rows exist for the given ordered tetrahedra index, and then re-create a longer row-wise but shorter column-wise (by 1 row) face matrix where its ordered by the last tetrahedra index column.

Any advice on how I can do this/is this even the best method to use you can give me would be appreciated! Thanks!

## Products

No products are associated with this question.

Answer by Sean de Wolski on 5 Dec 2012

Well first, you'll have to use hex2dec to get the hexadecimals to decimals.

Second, can you make a smaller example of what you expect for outputs. E.g.:

18f 1b6 368 1 db3

Should become what?

```doc hex2dec
doc sort
```

to get you started

Brian on 5 Dec 2012

Hello, thanks for getting back to me,

I generated a smaller test case, which is a lot easier to organize.

Basically, I would like to go from (I already translated to decimal):

``` 99      95	120	1	265
99	89	95	1	156
95	89	120	1	68
99	120	89	1	88
113	109	87	2	290
113	106	109	2	99
109	106	87	2	352
113	87	106	2	59```

to:

``` 99	95	120	1
99	89	95	1
95	89	120	1
99	120	89	1
113	109	87	2
113	106	109	2
109	106	87	2
113	87	106	2```

, where the forth column is the tetrahedra index. What makes this a bit more complicated than simply removing the 5th column is that, in the first matrix with 5 columns, one of the 3 (or 4) tetrahedra indices for tetrahedra 256, 156, 68, 88, etc is in the first 5 rows. This means that I want to duplicate this line: one for tetrahedra index 1, and 1 for tetrahedra index 256, etc.

The first 20 or so lines are easy, but as you go further int the file, they get more jumbled. The reason why there are 2 tetrahedra indices for each triangle is because on each side of the triangle, there is a volume for the tetrahedra. 1 face covers two tetrahedra, so I am going to nearly double the rows of my face matrix.

John Petersen on 5 Dec 2012

I don't see a duplication in the 2nd matrix that you're talking about.

Brian on 5 Dec 2012

What I meant by this was I wanted to sort from 1 to N in column 4, and line 1 would be repeated for tetrahedra 1 and 256. So I would just about duplicate the number of rows.

I think I managed to do this by:

``` facemx1 = faceMx(:,1:4);
facemx2 = faceMx(:,1:3);
facemx2 = [facemx2 faceMx(:,5)];
completeFaceMx = [facemx1;facemx2];
sortrows(completeFaceMx,4);
clear facemx1 facemx2```

So, basically, I create two new matrices with one of the tetrahedra indexes (col4 or 5), I concatenate the matrices, the sort by the forth column. It's not exactly what I envisioned, and it wastes memory (esp for larger files), but it works.

Thanks!