Error regarding deletion of an entry from structured array

5 views (last 30 days)
Hi,
I am performing polygon analysis from some pictures. I have the vertices and the respective distances among these vertices for all polygons. Now i want to filter out the vertices that are close to each other accrding to a threshold value and remove these vertices. So far i have been able to remove the entries from the distances matrix but the vertices at the same index are not getting removed. I get the error
''Index in position 1 is invalid. Array indices must be positive integers or logical values.
coords(g,:) = []; ''
Here is my code so far:
for i=1:nFloes
diss = vertcat(Dist_Ver{i});
for g =(length(diss)):-1:1
if (diss(g) < 5)
diss(g) = []; %%this thing works fine
g=g-1;
coords(g,:) = []; %% But the entries in here are not getting deleted even if the index is the same
end
end
% New_Dist_Ver{i} = diss;
end
If i run the same code for a single polygon, it works fine but isn't working if i apply it for all the polygons. Any help will be highly appreciated. Thanks.
  3 Comments
Muhammad Bilal Khawar
Muhammad Bilal Khawar on 2 Jan 2021
Hi! I solved the issue. Actually i was not initializng the coords arrays using vertcat in the loop in beginning.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Jan 2021
for g =(length(diss)):-1:1
Looping backwards is a good idea when deleting data from an array.
g=g-1;
You are modifying the loop index. That is seldom a good idea.
coords(g,:) = [];
The for loop gets down to 1. You subtract 1, getting 0. You try to delete row 0.
  8 Comments
Muhammad Bilal Khawar
Muhammad Bilal Khawar on 3 Jan 2021
Edited: Muhammad Bilal Khawar on 3 Jan 2021
Hi Walter!! So i have been trying to grasp what you said in your last comment. Now i am comparing the magnitude of vectors from A array to the B in a way that one value is getting compared with all the values from B array and after that second value being compared with all the values of B array and so on. And this works fine. But when my condition that if magnitude of array B(i) < A(i), the code should remove the respective vertix it is getting compared to. Here is the code:
for r = length(mag_v1):-1:1
for t = r:-1:1
distancert(t) =(abs(mag_v1(r)-mag_v2(t)));
if (distancert(t)<5)
coords(t,:) = [];
end
end
end
I am getting the same error for coords(t,:) = []; saying the indexing gets to unreal or non logical value. Can you help me out ????
Secondly, in this code, the values from A are compared with all the values of B. But if i want to just compare the values of A starting from the same index of array B. How should i do that ??
Any help will be highly appreciated.
mag_v1 = [20.6 8.4 15 2.2 5.6 2.2 20 15.8 4.4 7.07 2.2 12 2.2 5.6 5.8 2.23 15]
mag_v2 = [8.4 15 2.2 5.6 2.23 20 15.8 4.4 7.07 2.23 12 2.23 5.65 5.83 2.215 20.6]
Walter Roberson
Walter Roberson on 3 Jan 2021
Each time you delete from coords, you do not make mag_v1 or mag_v2 shorter. You can end up deleting the same t slot several times.
What are your mag_v1 and mag_v2 vectors representing?
Did you do a pdist() to determine the distance of each point to each other point? What order of magnitude of the number of vertices are you working with? Is your distance Euclidean based upon polygon centroids? For a few hundred polygons, pdist() is probably the easiest to deal with, but if the number of polygons rises enough then it perhaps becomes more time effective to do kd-tree range search iteratively.
  • build a kd-tree
  • ask to range-search distance 5 around each point.
  • initialize a Keep list the same length as the number of vertices, all true
  • iterate over the vertices. If the current vertex is marked to be deleted (Keep is false), continue to the next vertex.
  • Otherwise, take the list of nearest vertices for this vertex and throw away the ones refering to an earlier vertex number, and mark whatever remains as false in the Keep list
  • At the end, you have a logical index of vertices to Keep. Extract only the coordinate rows corresponding.
Notice that when you reach a node and it is marked as Keep false, that you do not examine its neighbor list. Thus in the example above A close to B, B close to C, A not close to C, you would proceed to mark B false, then you would iterate and see that B is false so you would ignore the information that it is close to C, and you would get to C and it would still be on the keep list; A and C are both Keep so you would extract them at the end.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!