Error regarding deletion of an entry from structured array
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Muhammad Bilal Khawar
le 2 Jan 2021
Commenté : Walter Roberson
le 3 Jan 2021
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 commentaires
Réponse acceptée
Walter Roberson
le 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 commentaires
Muhammad Bilal Khawar
le 3 Jan 2021
Modifié(e) : Muhammad Bilal Khawar
le 3 Jan 2021
Walter Roberson
le 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.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!