## Is there any way to speed up the function `changem`?

I am doing some substitutions of the index of triangle mesh. After removing or selecting some points on the mesh, I need to "retrench" the connectivitylist. I always using changem to achieve this. Like below,

function TR1 = simpleTR(TR)

node_idx = unique(TR.ConnectivityList);

node1 = TR.Points(node_idx,:);

node_idx1 = 1:size(node1,1);

face1 = changem(TR.ConnectivityList,node_idx1′,node_idx);

TR1 = triangulation(face1,node1);

end

I found the speed of this is slow when the input TR is big. In profiling, the code in changem below takes the most of time.

B(A == oldval(k)) = newval(k);

Is there any way to accelerate this?

———————-edit for @Cam Salzberger comment———————————————

I would like to clarify my problem in two aspects. One is the bigger picture, which is what I would like to do with the codes. The other is the problem of changem.

1. The goal of my codes is to make a new triangle mesh. The new mesh has fewer vertices(TR.Points) than the original one, after I deleted some vertex of it. The index of the faces (TR.ConnectivityList) should be "retrench", that is, the range of the faces (TR.ConnectivityList) will be smaller. See the example below, which is from matlab page,triangulation

% the original mesh

P = [ 2.5 8.0

6.5 8.0

2.5 5.0

6.5 5.0

1.0 6.5

8.0 6.5];

T = [5 3 1;

3 2 1;

3 4 2;

4 6 2];

TR = triangulation(T,P)

triplot(TR)

Now I delete the 5th vertex. The new mesh should be,

T_new = [ 3 2 1;

3 4 2;

4 6 2];

TR1a = triangulation(T_new,P);% some vertices is not referenced

TR1b = simpleTR(TR1a); % the content is above

%>> TR1b.Points

ans =

2.5 8

6.5 8

2.5 5

6.5 5

8 6.5

%>> TR1b.ConnectivityList

ans =

3 2 1

3 4 2

4 5 2

In the process, I need to replace "6" with "5" with changem. In practise, I have to changem many number for a large mesh.

2. For the problem of changem. In this function, I found it did a loop for "find" and "change value" combination.

B(A == oldval(k)) = newval(k);

I believe it has some chance to improve. Because it seems in every loop, it traverses through all of the elements in the matrix(TR.ConnectivityList). I think this way may be imperfect. It may use some hashtable-like thing to do it., which could be done in one loop.

All of this is my guess. Thank you for any suggestions.I am doing some substitutions of the index of triangle mesh. After removing or selecting some points on the mesh, I need to "retrench" the connectivitylist. I always using changem to achieve this. Like below,

function TR1 = simpleTR(TR)

node_idx = unique(TR.ConnectivityList);

node1 = TR.Points(node_idx,:);

node_idx1 = 1:size(node1,1);

face1 = changem(TR.ConnectivityList,node_idx1′,node_idx);

TR1 = triangulation(face1,node1);

end

I found the speed of this is slow when the input TR is big. In profiling, the code in changem below takes the most of time.

B(A == oldval(k)) = newval(k);

Is there any way to accelerate this?

———————-edit for @Cam Salzberger comment———————————————

I would like to clarify my problem in two aspects. One is the bigger picture, which is what I would like to do with the codes. The other is the problem of changem.

1. The goal of my codes is to make a new triangle mesh. The new mesh has fewer vertices(TR.Points) than the original one, after I deleted some vertex of it. The index of the faces (TR.ConnectivityList) should be "retrench", that is, the range of the faces (TR.ConnectivityList) will be smaller. See the example below, which is from matlab page,triangulation

% the original mesh

P = [ 2.5 8.0

6.5 8.0

2.5 5.0

6.5 5.0

1.0 6.5

8.0 6.5];

T = [5 3 1;

3 2 1;

3 4 2;

4 6 2];

TR = triangulation(T,P)

triplot(TR)

Now I delete the 5th vertex. The new mesh should be,

T_new = [ 3 2 1;

3 4 2;

4 6 2];

TR1a = triangulation(T_new,P);% some vertices is not referenced

TR1b = simpleTR(TR1a); % the content is above

%>> TR1b.Points

ans =

2.5 8

6.5 8

2.5 5

6.5 5

8 6.5

%>> TR1b.ConnectivityList

ans =

3 2 1

3 4 2

4 5 2

In the process, I need to replace "6" with "5" with changem. In practise, I have to changem many number for a large mesh.

2. For the problem of changem. In this function, I found it did a loop for "find" and "change value" combination.

B(A == oldval(k)) = newval(k);

I believe it has some chance to improve. Because it seems in every loop, it traverses through all of the elements in the matrix(TR.ConnectivityList). I think this way may be imperfect. It may use some hashtable-like thing to do it., which could be done in one loop.

All of this is my guess. Thank you for any suggestions. I am doing some substitutions of the index of triangle mesh. After removing or selecting some points on the mesh, I need to "retrench" the connectivitylist. I always using changem to achieve this. Like below,

function TR1 = simpleTR(TR)

node_idx = unique(TR.ConnectivityList);

node1 = TR.Points(node_idx,:);

node_idx1 = 1:size(node1,1);

face1 = changem(TR.ConnectivityList,node_idx1′,node_idx);

TR1 = triangulation(face1,node1);

end

I found the speed of this is slow when the input TR is big. In profiling, the code in changem below takes the most of time.

B(A == oldval(k)) = newval(k);

Is there any way to accelerate this?

———————-edit for @Cam Salzberger comment———————————————

I would like to clarify my problem in two aspects. One is the bigger picture, which is what I would like to do with the codes. The other is the problem of changem.

1. The goal of my codes is to make a new triangle mesh. The new mesh has fewer vertices(TR.Points) than the original one, after I deleted some vertex of it. The index of the faces (TR.ConnectivityList) should be "retrench", that is, the range of the faces (TR.ConnectivityList) will be smaller. See the example below, which is from matlab page,triangulation

% the original mesh

P = [ 2.5 8.0

6.5 8.0

2.5 5.0

6.5 5.0

1.0 6.5

8.0 6.5];

T = [5 3 1;

3 2 1;

3 4 2;

4 6 2];

TR = triangulation(T,P)

triplot(TR)

Now I delete the 5th vertex. The new mesh should be,

T_new = [ 3 2 1;

3 4 2;

4 6 2];

TR1a = triangulation(T_new,P);% some vertices is not referenced

TR1b = simpleTR(TR1a); % the content is above

%>> TR1b.Points

ans =

2.5 8

6.5 8

2.5 5

6.5 5

8 6.5

%>> TR1b.ConnectivityList

ans =

3 2 1

3 4 2

4 5 2

In the process, I need to replace "6" with "5" with changem. In practise, I have to changem many number for a large mesh.

2. For the problem of changem. In this function, I found it did a loop for "find" and "change value" combination.

B(A == oldval(k)) = newval(k);

I believe it has some chance to improve. Because it seems in every loop, it traverses through all of the elements in the matrix(TR.ConnectivityList). I think this way may be imperfect. It may use some hashtable-like thing to do it., which could be done in one loop.

All of this is my guess. Thank you for any suggestions. speed, changem, index MATLAB Answers — New Questions