7 views (last 30 days)

% 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!!!!!!!

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.

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.

Opportunities for recent engineering grads.

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/503983-need-help-to-delete-garbage-values-from-a-matrix#comment_794153

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/503983-need-help-to-delete-garbage-values-from-a-matrix#comment_794153

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/503983-need-help-to-delete-garbage-values-from-a-matrix#comment_794157

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/503983-need-help-to-delete-garbage-values-from-a-matrix#comment_794157

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/503983-need-help-to-delete-garbage-values-from-a-matrix#comment_794158

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/503983-need-help-to-delete-garbage-values-from-a-matrix#comment_794158

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/503983-need-help-to-delete-garbage-values-from-a-matrix#comment_794175

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/503983-need-help-to-delete-garbage-values-from-a-matrix#comment_794175

Sign in to comment.