Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Often, certain data entries in a data set can be missing, corrupted, or invalid.

ID: 3799185 • Letter: O

Question

Often, certain data entries in a data set can be missing, corrupted, or invalid. Computers often assign such entries with value of NaN (which stands for not-a-number). In MATLAB, the NaN functions similarly to numbers. For example, you could create a MATLAB array 12:5 NaN 7 81. To see an important attribute of NaN that is different than a number, type NaN NaN into the command window and observe the result. The below steps require you to use MATLAB features that can be helpful when using data sets. Follow the steps below: Note: do not present any of the arrays in the command window unless requested. Create a 2D data array using the lines below (write them exactly as written below): rng(2); line 1 A = randi(10, 1000, 1000); line 2 A = (randi (1e6, 20, 1)) NaN; %line 3 Describe in words (in the command window) what line 3 in part a does. Clearly present the indices of the columns of A which contain a NaN. Replace any element of Athat is NaN with a 0. e. Certain elements of A have the following characteristics: an even row number, an odd column number, and a value that is less than or equal to 5. Replace every element which has every listed attribute with a value of -41. Clearly report the sum of the values in A (1 number) in the command window.

Explanation / Answer

a. rng(2); % line 1

A = randi(10,1000,1000); % line 2

A(randi(1e6,20,1)) = NaN; % line 3

Generates a 2D array using randi function

Eg. Consider a 2D array of 5x5

A = randi(10,5,5) % line 2

A(randi(2,5,1)) = NaN % line 3

b. A(randi(1e6,20,1)) = NaN;

Adds 20 NaN in an array at random indices.

Eg.

A(randi(10,3,1)) = NaN

c. To displays the indices of NaN in A:

A = randi(10,5,5); % line 2

A(randi(10,3,1)) = NaN % line 3

for i = 1:5

for j = 1:5

if (isnan(A(i,j)) == true)

fprintf('%d %d ',i,j);

j = j + 1;

end

end

end

d. Replacing any element of A that is NaN with a 0:

A = randi(10,5,5); % line 2

A(randi(10,3,1)) = NaN % line 3

for i = 1:5

for j = 1:5

if (isnan(A(i,j)) == true)

fprintf('%d %d ',i,j);

j = j + 1;

end

end

end

t = 0;

for i = 1:5

for j = 1:5

if (isnan(A(i,j)) == true)

A(i,j) = 0

t = 1;

break;

end

end

if (t == 1)

break;

end

end

e. Replacing every element that has an even row number, an odd column number, and a value that is less than or equal to 5 with -41

A = randi(10,5,5); % line 2

A(randi(10,3,1)) = NaN % line 3

for i = 1:5

for j = 1:5

if (isnan(A(i,j)) == true)

fprintf('%d %d ',i,j);

j = j + 1;

end

end

end

t = 0;

for i = 1:5

for j = 1:5

if (isnan(A(i,j)) == true)

A(i,j) = 0;

t = 1;

break;

end

end

if (t == 1)

break;

end

end

% replace by -41

t = 0;

for i = 1:5

for j = 1:5

if (rem(i,2) == 0 && rem(j,2) == 1 && 5 >=A(i,j))

A(i,j) = -41

t =t+1;

end

end

end

f. Adding all the values of A ignoring NaN:

y = nansum(A)

l=sum(y)

Final code:

rng(2); % line 1

A = randi(10,1000,1000); % line 2

A(randi(1e6,20,1)) = NaN; % line 3

for i = 1:1000

for j = 1:1000

if (isnan(A(i,j)) == true)

fprintf('%d %d ',i,j);

j = j + 1;

end

end

end

% replace a Nan with 0

t = 0;

for i = 1:1000

for j = 1:1000

if (isnan(A(i,j)) == true)

A(i,j) = 0;

t = 1;

break;

end

end

if (t == 1)

break;

end

end

% Replaces every element that has an even row number, an odd column number, and a value that is less than or equal to 5 with -41

t = 0;

for i = 1:1000

for j = 1:1000

if (rem(i,2) == 0 && rem(j,2) == 1 && 5 >=A(i,j))

A(i,j) = -41;

t =t+1;

end

end

end

%Adding all the array values ignoring NaN

y = nansum(A)

l=sum(y)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote