Write a function to calculates and returns the number of pos
Write a function to calculates and returns the number of positive, negative and zero elements in a vector x. Solution function [noP, noZ, noN] = countNumbers(x) % noP is the number of positive elements in x. % noZ is the number of zero elements in x. % noN is the number of negative elements in x. % write your code here y = x; end
Solution
function [noP,noZ,noN]=countNumbers(x)
y=x;
noP=0;
noZ=0;
noN=0;
[r c]=size(y);%returns number of rows and columns
for i=1:c%taking the length of vector
if y(i)>0%checks for positive values
noP=noP+1;
elseif y(i)<0%checks for negative values
noN=noN+1;
else%checks for zeros
noZ = noZ+1;
end
end
end
Command Window output:
>> [noP,noZ,noN]=countNumbers([1 5 -6 0 2 -5 0 0])
noP =
3
noZ =
3
noN =
2
>>
![Write a function to calculates and returns the number of positive, negative and zero elements in a vector x. Solution function [noP, noZ, noN] = countNumbers(x Write a function to calculates and returns the number of positive, negative and zero elements in a vector x. Solution function [noP, noZ, noN] = countNumbers(x](/WebImages/26/write-a-function-to-calculates-and-returns-the-number-of-pos-1066648-1761558049-0.webp)