Error regarding deletion of an entry from structured array

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

Make it easy for us to help you.
Can you attach nFloes and Dist_Ver in a .mat file
save('answers.mat', 'nFloes', 'Dist_Ver');
then use the paper clip icon.
nFloes is just number of floes which is 84.
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

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

Thanks for your reply. Yes i think i know this one. But the problem is even if i delete the row using coords(g,:) = [];
before subtracting the counter g, the error stays and somehow g never goes down to one. Can you suggest how should i run this loop ??
If i put the counter after i have deleted the rows from both diss and coords matrix and then decrease the counter, the error stays and somehow the rows gets messed up. And another troubling thing is, this loop works fine for a single polygon. But fails when i try to run it for the whole bunch of polygons.
Why are you decreasing the counter yourself? The for loop is decreasing it on your behalf.
Okay so lets say initial length of last diss array is 17 and coords is 18.
If i don't use g=g-1; the error comes and the final counter g is 12 and i end up with diss(length) = 15 and coords(length) = 14. This is wrong. The correct answer should be diss(length) = 9 and coords(length) = 10 i-e total vertices reduced to 10.
Question: if B is distance 4 from A and C is distance 4 from B but C is more than 4 from A then should C be deleted? Or can you somehow do a topological sort and proceed iteratively, deleting everything too close to the first point, and then deleting everything that is too close to the second point of what is left?
The loop problem is solved. I was not using vertcat for coords arrays. Instead i was just doing it for distance. Now i get the result.
To answer your question ::: So you mean i should compare the first vertex with the next untill the required distance is more than threshold and keep that vertex unless delete it ??
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]
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!