Filter löschen
Filter löschen

i dont know how i can take out the max and min value from the vector

2 Ansichten (letzte 30 Tage)
Write a MATLAB script that calculates the average of the values in an input vector. However, your script should exclude the maximum and minimum values from calculations. Assume that the values in the vector are unique.
  5 Kommentare

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Dez. 2021
V=[0 1 20 35 56]
Okay, V will be created in the "base workspace" and assigned that list of values.
avgfunc(V);
And there it will be passed to avgfunc(). If avgfunc() returns any values then display of the values will be supressed because of the semi-colon at the end of the call
function avgfunc(input)
Okay, inside avgfunc(), whatever value has been passed into the function will be known by the nickname input
Mean=mean(V);
That is going to start a search looking for a definition for V . V has not been assigned to yet in the current function, so it is not a local variable. V is not the local name of any parameter to the function -- the one parameter passed in is known as input here. The function is not a "nested function" so V cannot be the name of a "shared variable".
So MATLAB is going to proceed to search for the name V as the name of a function, with there being a well-defined list of places it looks, each with its own priority. But V does not happen to be the name of any function on the seach path, so V will be considered undefined here.
Inside a function if you use the name of a variable that is defined in the "base workspace" or in the calling function, MATLAB will not search the base workspace or the calling function to find a matching variable.
If you wanted the value of the parameter that was passed in, you should have used input in the call instead of V
Beyond that: you are taking the mean of the entire vector. But the problem requires that you remove the minimum and maximum from the vector and take the mean of what remains.
avgfung(V)=Mean;
That would attempt to create a local variable named avgfung indexed by the values stored in V and assign those locations the values stored in Mean . If you were to use input instead of V to get around the problems explained a moment ago, you would encounter the problem that the input contains the value 0, but 0 is not a valid subscript in MATLAB.
I notice that your variable name avgfung is very similar to the name of the function avgfunc . If you were intending to assign to the same name as the function, then please note that in MATLAB, assigning to the name of the function is not the way to return values from the function. If you want to return values from the function, you need to list variable names on the left side of an "=" in the function line, such as
function Mean = avgfunc(input);
  3 Kommentare
Walter Roberson
Walter Roberson am 22 Dez. 2021
Hint: if you were to sort the vector, then the values to discard would be the first and the last elements of the sorted values.
Asli
Asli am 22 Dez. 2021
Thank you very much, I wrote everything from scratch.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by