Write a function with the following header function npositiv

Write a function with the following header: function [n_positive, n_negative, n_zero, n special] = my array metrics num (array) where: array is an arbitrary n times m array of class double. n_positive, n _negative, n_zero, and n_special are scalars of class double that represent, respectively, the number of strictly positive, strictly negative, zero, and \"special\" values in array. Special values are NaN, Inf, and -Inf. Note that Inf is both positive and \"special\", and -Inf is both negative and \"special\" Test cases: >>[[n _positive n_negative n_zero, n_special] = my array metrics num ([10]) n positive = 1 n negative = theta n_zero = theta n_special = theta >>array = (5, 0, 0, 1, 1, 7]; >>[n positive, n_negative, n_zero, n _special] = my array metrics num (array) n_positive = 3 n_negative = 1 n_zero =

Solution

% matlab code

function[n_positive, n_negative, n_zero, n_special] = my_array_metrics_num(array)
n_positive = 0;
n_negative = 0;
n_zero = 0;
n_special = 0;
[n,m] = size(array);
for i=1:n
for j=1:m
if array(i,j) == Inf
n_positive = n_positive + 1;
n_special = n_special + 1;
elseif array(i,j) == -Inf
n_negative = n_negative + 1;
n_special = n_special + 1;
elseif array(i,j) == NaN
n_special = n_special + 1;
elseif array(i,j) < 0
n_negative = n_negative + 1;
elseif array(i,j) > 0
n_positive = n_positive + 1;
elseif array(i,j) == 0
n_zero = n_zero + 1;
  
end
end
end
end

array = [5, 0 , 0, Inf, -1 , 7];
[n_positive, n_negative, n_zero, n_special] = my_array_metrics_num(array);
disp(\"n_positive = \");
disp(n_positive);
disp(\"n_negative = \");
disp(n_negative);
disp(\"n_zero = \");
disp(n_zero);
disp(\"n_special = \");
disp(n_special);

%{
output:

n_positive =
3
n_negative =
1
n_zero =
2
n_special =
1

%}

 Write a function with the following header: function [n_positive, n_negative, n_zero, n special] = my array metrics num (array) where: array is an arbitrary n
 Write a function with the following header: function [n_positive, n_negative, n_zero, n special] = my array metrics num (array) where: array is an arbitrary n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site