In: Computer Science
[In MATLAB] We have an array of seven values for a number x : [ 1.92, 0.05, -2.43, -0.02, 0.09, 0.85 . 0.06] . We consider any data value in the range -0.1 <x<0.1 as erroneous value. We want to remove all such values and replace them with zeros at the end of the array.
MATLAB Code:
% Code for part a
x = [ 1.92, 0.05, -2.43, -0.02, 0.09, 0.85 , 0.06];
result = [];
% err variable stores the count of erroneous value
err = 0;
% If erroneous value is found then increment by 1 else append the value to
% the result array
for i = 1:7
if (x(i) < 0.1) && (x(i) > -0.1)
err = err + 1;
else
result(end+1) = x(i);
end
end
% Fill rest with zeros
for i = 1:err
result(end+1) = 0;
end
% Display the result
fprintf("The modified array for part a is:")
disp(result);
% Code for part b
x = [ 1.92, 0.05, -2.43, -0.02, 0.09, 0.85 , 0.06];
% Find the indexes of erroneous values
k = find( (-0.1 < x) & (x < 0.1) );
% err variable stores the count of erroneous value
err = length(k);
% Remove those erroneous values
x(k) = [];
% Append rest with zeros
x = [x zeros(1,err)];
% Display the output
fprintf("The modified array for part b is:")
disp(x);
Please refer to the following picture for sample execution of the above code: