Question

Write a MATLAB function function [ AvgPos, AvgNeg ] = PosNegAverage ( X ) that calculates...

Write a MATLAB function
function [ AvgPos, AvgNeg ] = PosNegAverage ( X )

that calculates average of positive, AvgPos, and negative, AvgNeg, elements of array X, using the for‐end loop and if‐elseif‐else‐end selection structure. Do not use build‐in MATLAN functions in calculations. Apply the developed function for the following vector

X = [ ‐7, 1, 0, 0, 12, 6, 33.2, ‐7.5 ];

Homework Answers

Answer #1

MATLAB Code:

close all
clear
clc

X = [-7, 1, 0, 0, 12, 6, 33.2, -7.5];
[AvgPos,AvgNeg] = PosNegAverage(X)

function [AvgPos,AvgNeg] = PosNegAverage(X)
AvgPos = 0;
AvgNeg = 0;
NumPos = 0;
NumNeg = 0;
for i=1:length(X)
if X(i) > 0
AvgPos = AvgPos + X(i);
NumPos = NumPos + 1;
else
if X(i) < 0
AvgNeg = AvgNeg + X(i);
NumNeg = NumNeg + 1;
end
end
end
if NumPos > 0
AvgPos = AvgPos/NumPos;
end
if NumNeg > 0
AvgNeg = AvgNeg/NumNeg;
end
end

Output:

AvgPos =
13.0500
AvgNeg =
-7.2500

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an R function named fifth with a single vector argument x that calculates and returns...
Write an R function named fifth with a single vector argument x that calculates and returns the sample mean of he 5th, 10th, 15th, ... elements of x. You may assume that x has at least 5 elements.
Write a MatLab function J = Jcb(f, x) that computes the jacobian of the vector f...
Write a MatLab function J = Jcb(f, x) that computes the jacobian of the vector f of functions of x. Input f is the vector of functions [f1(x1, x2, x3...); f2(x1, x2, x3...); ...] and inout x is a vector of unkown x [x1; x2; x3;...]. Output J is the jacobian square matrix of vector f.
Write a MatLab code J = Jcb(X) that computes the jacobian of a nonlinear vector of...
Write a MatLab code J = Jcb(X) that computes the jacobian of a nonlinear vector of functions of X. Input X is a vector of unkown functions of X and output J is the jacobian of the nonlinear vector function of X.
IN MATLAB: Write a function file that takes a vector as an input and returns another...
IN MATLAB: Write a function file that takes a vector as an input and returns another vector with all repeated elements of the original vector. For example, the vector [3 4 1 0 4 -5 7 3] would return the vector [3 4]. Do not use the built-in function "repelem."
Using for loop and if statement, write a MATLAB code to plot a graph for x(t)...
Using for loop and if statement, write a MATLAB code to plot a graph for x(t) as a function of time t in the range 0 to 12 in increment of 0.01 ?(?) = 1: 0 ≤ ? ≤ 1 2? − 1 1 ≤ ? ≤ 2 3 2 ≤ ? ≤ 3 −2.5? + 10.5 3 ≤ ? ≤ 5 −2 5 ≤ ? ≤ 6 4/3 ? − 10 6 ≤ ? ≤ 9 2 9 ≤...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an n-by-n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note that...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
Write a Matlab program to plot the cosine wave. That is plot y=cos(k1x) vs. x for...
Write a Matlab program to plot the cosine wave. That is plot y=cos(k1x) vs. x for x varying from 0 to 2pi radians. The value of k1 is given at the end of this document. You can choose the increment for x. Note that large values of the increment will give you a coarse graph. Note: Matlab has two functions to calculate the sine of an angle: sin(x) and sind(x). What is the difference between these two functions? Use help...
How can this PHP code be changed to use a parameter to the function that you...
How can this PHP code be changed to use a parameter to the function that you would then pass $students in? (instead of using a global). Also, can you show how to use a loop to handle any length of array? (instead of an array that is 3 elements). <?php $students = array(     array('FName'=>'Jane', 'LName'=>'Doe', 'ClassScore'=>90),     array('FName'=>'Bob', 'LName'=>'Joe', 'ClassScore'=>50),     array('FName'=>'John', 'LName'=>'Doe', 'ClassScore'=>20)     );     //1. CalculateSum() Function     function CalculateSum()     {         // Use $students array throughout the function.         global $students;         // Add...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT