- create a logical vector mask that indicates whether an element is to be kept (true) or not (false). Then at the end, newpoints = newpoints(mask); OR
- delete backwards . End towards the beginning. If you do that, then anything that "falls down" is data that has already been examined and does not need to be examined again, and it does not matter that the array gets shorter since you still have the same number of elements to examine heading towards the beginning.
Delete every point that is nearer than a variable
1 view (last 30 days)
Show older comments
Hello everyone,
I'm trying to delete every point that is nearer than the variable vel_over, in this example 1.4mm, to the extreme values (these are around 339mm and 439mm). This program worked fine until I tried to change the duration of the period. The error message says that the index exceeds the number of array elements and I tried to just delete the last element, but it still doesn't work (the last element suddendly appears again). Can someone help me and find the mistake in my code? I'm thankful for all your ideas.
clear;
vel_over = 1.4;
A = 50; %Amplitude
t = 0.05; %Frequenz
off = 389.387; %Offset von x-Achse
vel_document = readtable("C:\Users\Bruce Rogers\Documents\RoboDK\1pi\RoboDK\Velocity_1pi.txt");
vel = vel_document{:,1};
factor = 0.5;
x = 0:t:(1/factor)*10*pi;
y = -A*sin(factor * x) + off;
T = (1:length(x))*t;
figure(1)
plot(T,y,'b.')
xlabel('time')
ylabel('distance')
hold on
%%
%y = y(:,1:end-1);
newpoints = zeros(1,(length(y)));
newpoints(1,1) = y(1,1);
%newpoints(1,2) = y(1,2);
z = zeros(1,(length(y)));
%z(numel(y)) = 0;
for i = 2:length(y)
z(i) = (y(i) - y(i-1));
end
z = abs(z);
for i = 1:length(z)
z(i) = z(i) < vel_over;
end
z(numel(y)) = 0;
z(1,1) = 0;
%y(isnan(y))=0;
%z(numel(y)) = 0;
%%
for i = 1:length(y)
%newpoints(i) = y(i);
if y(i) > 339.30 && y(i) < 339.45
j = i;
while z(j) == 1 %%The error message appears here
y(j+2) = NaN;
j = j + 2;
end
j = i;
while z(j) == 1
y(j-2) = NaN;
j = j - 2;
end
end
if y(i) > 439.33 && y(i) < 439.39
j = i;
while z(j) == 1
y(j+2) = NaN;
j = j + 2;
end
j = i;
while z(j) == 1
y(j-2) = NaN;
j = j - 2;
end
end
end
newpoints(1,1) = y(1,1);
for i = 2:length(y)
newpoints(i) = y(i);
while isnan(y(i))
vel(i) = ((vel(i)+vel(i-1))/2);
%vel(i-1) = 0;
if isnan(newpoints(i))
newpoints(i) = [];
end
i = i + 1;
end
end
figure(1)
plot(T,newpoints,'r.')
xlabel('time')
ylabel('distance')
newpoints = newpoints';
%xlswrite('neue_punkte.xlsx',newpoints)
%xlswrite('neue_geschwindigkeit.xlsx',vel)
0 Comments
Answers (1)
Walter Roberson
on 16 Jun 2021
Edited: Walter Roberson
on 16 Jun 2021
while isnan(y(i))
vel(i) = ((vel(i)+vel(i-1))/2);
%vel(i-1) = 0;
if isnan(newpoints(i))
newpoints(i) = [];
end
i = i + 1;
After you delete a point, newpoints is now shorter, but you continue to treat it as if it were the original length.
Remember that deleting an element in a matrix is like Tetris: something "falls down" to occupy the slot. If you have an array [1 2 3 4] and you delete 3 then the array becomes [1 2 4] with 4 moving into the space where 3 was, and the vector no longer being 4 long. And you increrement your focus to position 4, but notice you never examined it! When you delete a point and you are moving forward, then you need to retain your focus at the same location, in order to examine the data that fell down into the current location.
There are two ways to proceed:
3 Comments
Walter Roberson
on 16 Jun 2021
You never delete anything from y. You assign NaN to some of the elements of y.
Your code is odd. Why do you do
z = abs(z);
for i = 1:length(z)
z(i) = z(i) < vel_over;
end
when you could just do
z = abs(z) < vel_over;
?
Or better yet, change variable names as you switch between z as some kind of distance, and z as some kind of mask.
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!