Develop a user-defined function entitled posNegSplit which accepts an array as a
ID: 3563709 • Letter: D
Question
Develop a user-defined function entitled posNegSplit which accepts an array as an input. The goal of the user-defined function is to .split the positive and negative values within the array into two newly created, distinct arrays in your file. Name these arrays pos and neg. respectively. If you encounter zeros in the array, do not include them into pos or neg. Instead, count the number of zeros in your input array using a variable named count_zeros. You will also need to create indices to keep track of the occurrences where you add a new element into either pos or neg. Name these indices p_index and n_index, respectively. When developing your function, start with the following framework and articulate the output as shown in the example: Notice the user-defined function supports two different outputs. Create the logic in your code so that it is robust enough to know whether or not to produce the zero comment. In other words, if there are no zero elements in the input array, then the code should report strictly the positive and negative values. Else. it should report all three without any code modification in either case. Execute your function using the following arrays:Explanation / Answer
function [a b n]=posNegSplit(inputArray)
l=length(inputArray);
n=0;
pos_count=1;
neg_count=1;
for i=1:l
if inputArray[i]>0
a[pos_count]=inputArray[i];
pos_count=pos_count+1;
else if inputArray[i]<0
b[neg_count]=inputArray[i];
neg_count=neg_count+1;
else if inputArray[i]==0
n=n+1;
end
end
end
end
disp('positive elements in the array : ')
disp(a)
disp('negative elements in the array: ')
disp(b)
if n~=0
x=['the input array contains ',num2str(n),' zeros/'];
disp(x)
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.