In: Computer Science
MATLAB QUESTION:
I have to write a function that pretty much cleans number.
these are the instructions
Inputs:
number1 - a 1d of array of number, size doesn't matter
outputs:
number2 - all number in number1 that are 0 need to be set to nan and return via number2.
data - storing the number of 0's in number1 that pretty much are being set to nans
here's the code:
function [number2, data] = cleanNumber(number1)
%add your code below..
end
code to call your function
rebuild = [ 205,201,198,0,197,210,220,199,0186,188];
disp("before cleaning numbers:")
disp(rebuild)
rebuild = cleanNumber(rebuild);
disp("after cleaning numbers:")
disp(rebuild)
Here is the function.
function [number2, data] = cleanNumber(number1)
number2=number1; % store the number1 in number2
number2(number2==0)=nan; % change the numbers with 0s to NaN
data = sum(number1==0); % finding the number of 0s
end
Running code for given conditions.
rebuild = [ 205,201,198,0,197,210,220,199,0186,188];
disp("before cleaning numbers:")
disp(rebuild)
rebuild = cleanNumber(rebuild);
disp("after cleaning numbers:")
disp(rebuild)
Output
before cleaning numbers:
205 201 198 0 197 210 220 199 186 188
after cleaning numbers:
205 201 198 NaN 197 210 220 199 186 188