Need help to delete garbage values from a Matrix
6 views (last 30 days)
Show older comments
% Create the .dat file
clc
format short
tout1=tout;
tout2=tout;
r=size(tout1);
n=r(1,1);
for i=2:n
if mod(tout1(i)-tout1(i-1), 0.00005) ~= 0
%if(tout1 > )
if mod(tout1(i), 0.00005) ~= 0
tout1(i) = 0;
else
tout1(i+1) = 0;
end
end
end
tout1 = unique(tout1);
tout=tout1;
the above code is written to delete garbage values that are being generated in the a matrix TOUT when i insert display block in simulink. But it makes every third row zero when the above code is implemented. so instead of getting 20001 values from 20005, i am getting 13386 values whic generates error saying that the dimentions of the matrix are not consistant.
why is it deleting extra values which satify the condition in the first loop?
Will like some help please!!!!!!!
4 Comments
Guillaume
on 6 Feb 2020
That's one of the many bizarre things about the code. Also the code appears to check that the difference between continuous numbers is a multiple of 0.00005 but later on that it's the numbers themselves that are multiple, which is a completely different thing. The difference between two numbers can be a multiple of 0.00005 without any of the numbers being a multiple.
Other bizarre thing, is that the difference is between index i and i-1 but it's i or i+1 that is later modified.
Accepted Answer
Guillaume
on 6 Feb 2020
First, as pointed out by David, your co
de doesn't take into account the properties of floating point numbers. I would strongly advise you read up about it, The floating point guide is a very good starting point. The consequence of it is that your code is not going to work the way you expect. Many numbers cannot be stored exactly and thus many numbers that you think should be a multiple of 0.00005 are not for a computer:
>> mod(0.0006 - 0.00055, 0.00005)
ans =
4.99999999999999e-05
See that for a computer (note that it's not restricted to matlab), the difference between 0.0006 and 0.00055 is not 0.00005. That's because neither 0.0006 nor 0.00055 can be stored exactly, 0.0006 is stored as a number slightly smaller (0.000599999999999999947437878677902745039318688213825225830078125 exactly) and 0.00055 as a number slightly larger (0.000550000000000000033133218391157015503267757594585418701171875) so the difference is slightly smaller than 0.0005 (off by ~8e-20).
So, as is your check is not going to work. That's not even taking into account the other bugs such as checking the difference between index i and i-1 but modifying index i or i+1.
Secondly, you still haven't spelled out what the criteria for garbage is. I'm going to take a guess and assume that all your numbers should be multiple of 0.0005 (taking into account precision of floating point numbers). Note that if numbers are multiple of 0.0005 it makes no sense checking if there difference is also a multiple of 0.0005, it's guaranteed.
%tout: a vector of number. The below removes numbers that are not multiple of 0.00005
tol = 1e-10; %abritrary tolerance below which numbers are considered equal
tout(abs(mod(tout, 0.00005)) > tol) = []
Note: 0.00005 is one of these rare numbers that can be stored exactly. So, in the above you could actually do an exact check (but not on the difference!). However, for most other divisors you can't, so it's better to always use a tolerance.
2 Comments
Guillaume
on 6 Feb 2020
"so i want to know how will i rectify it."
If the signal values must be multiple of 0.00005, then the code I've written above will do it:
%tout: a vector of number. The below removes numbers that are not multiple of 0.00005
tol = 1e-10; %abritrary tolerance below which numbers are considered equal
tout(abs(mod(tout, 0.00005)) > tol) = []
it indeed removes the first 5 values in your demo file.
If you want the difference between consecutive values to be in step of 0.00005 then I'm not sure what you want to be done when it's not the case. Unless they're at the beginning or the end, removing values is certainly not going to bring you back your 0.00005 step.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!