Reorganize vector elements without losing neighbor dependencies?

2 views (last 30 days)
I have the following problem: I have a square mesh of which I know all points and all their neighbor points. Saved as y, z, P, PNorth, PEast, PSouth, PWest. Here is an example with just a few points:
Now I put a shape in this mesh but not all of the points are in this shape. So I want to delete the entries that are not inside the shape, but without losing the neighbor point dependencies.
I was thinking of some kind of substitution with numbers, is that possible?
%the neighbor point in the north of point number 4 is number 5
P = [0, 0, 0, 4, 5, 0, 7, 0];
PNorth = [0, 0, 0, 5, 0, 0, 0, 0];
%now i would like to delete the zero entries, without losing these
%dependencies. Point number 4 should have index 1 etc...
old = [4,5,7];
new = [1,2,3];
subs(P,old,new); % (this does not worked, but it's like what it should do)
subs(PNorth,old,new);
%and that's the result
P = [1, 2, 3];
PNorth = [2, 0, 0];
Any idea how that could work? It should work fast for larger amounts of points, so i can save memory.

Accepted Answer

Jan
Jan on 22 Sep 2021
P = [0, 0, 0, 4, 5, 0, 7, 0];
PNorth = [0, 0, 0, 5, 0, 0, 0, 0];
old = [4,5,7];
new = [1,2,3];
LUT(old + 1) = new; % +1 to consider 0 elements
P = LUT(P + 1); % A look-up table
PNorth = LUT(PNorth + 1);
% Maybe:
keep = (P ~= 0 | PNorth ~= 0);
P = P(keep)
P = 1×3
1 2 3
PNorth = PNorth(keep)
PNorth = 1×3
2 0 0

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!