Write a function fml(x) where given vector x returns first, middle and last. If the number of elements in the vector are even, middle should take the mean value of the innermost elements.

5 views (last 30 days)
function [first, middle, last] = fml(x)
%FML Returns the first, middle, and last element of x
first = x(1);
last = x(length(x));
middle = x(length(x))/2;
if mod(length(x),2)==1
middle = (x(length(x)/2+1)+x(length(x)/2-1))/2;
elseif mod(length(x),2)==0
middle = x(length(x))/2;
end
--------- It is not working for some reason. Getting errors at the IF sequence.

Accepted Answer

John Chilleri
John Chilleri on 20 Sep 2017
Hello,
Let's take a look at your code,
function [first, middle, last] = fml(x)
%FML Returns the first, middle, and last element of x
first = x(1);
last = x(length(x));
middle = x(length(x))/2;
if mod(length(x),2)==1
middle = (x(length(x)/2+1)+x(length(x)/2-1))/2;
elseif mod(length(x),2)==0
middle = x(length(x))/2;
end
Your computation of first and last seems to be good; however, there is a problem with your computation of middle. First, you don't need the line,
middle = x(length(x))/2;
as you will assign a different value to middle anyways.
Next, let's consider your code,
if mod(length(x),2)==1
middle = (x(length(x)/2+1)+x(length(x)/2-1))/2;
elseif mod(length(x),2)==0
middle = x(length(x))/2;
end
Here you are checking if the length is odd or even using modulus then determining the value of middle based on that. However, you have two problems:
1) if mod(length(x),2) == 1, we know the length is odd, meaning that,
middle = (x(ODD/2+1)+x(ODD/2-1))/2;
ODD/2 will produce a something and 1/2 result, which is not a valid index. Furthermore, if it has an odd length, then we know that it does in fact have a middle value [1 2 3] has middle value 2 for example. Try changing the line to,
middle = x(ceil(length(x)/2));
Then, when the modulus = 0, we know it's even, so try using the line,
middle = (x(length(x)/2) + x(length(x)/2+1))/2;
I hope this helps!
  2 Comments
Delshad Ayoubi
Delshad Ayoubi on 20 Sep 2017
Regarding the last line where the modulus = 0, it is even:
middle = (x(length(x)/2) + x(length(x)/2+1))/2;
Doesn't
(x(length(x)/2)
return a value that is odd, since we know it is an even array of elements? Why isn't Ceil used in this case but prior to this one, when the modulus = 1?
Another question regards
x(length(x)/2+1))/2;
The addition of 1, does that mean you take the element next to the mean value? Can you explain this part to me thoroughly? I'm new to programming.
John Chilleri
John Chilleri on 20 Sep 2017
Hello,
So if modulus = 0, then we know you have an even number of elements, so length(x) is an even number. We know that you can divide any even number by 2, so we don't need to round it in any way.
For example,
if length(x) = 4
length(x)/2 = 2
if length(x) = 6
length(x)/2 = 3
So we can see it will always result in an integer.
Furthermore, the (length(x)/2+1) would be 3 and 4 for the two above examples.
So if you have a four element vector x = [1 2 3 4], we can see the mean will be the average of the second and third elements. And you can get the second by length(x)/2 = 2, and the third by (length(x)/2+1) = 3 then averaging the two. You can check this with a six element vector to see the same result.
On the other hand, when the modulus = 1, we know that it has an odd number of elements, and there is a middle element. For example, [1 2 3] has middle element 2 and [1 2 3 4 5] has middle element 3. We can see that the second and third elements of these vector represent the middle, and this can be obtained by ceil(length(x)/2) which is:
if length(x) = 3
ceil(length(x)/2) = ceil(1.5) = 2 which is the middle
if length(x) = 5
ceil(length(x)/2) = ceil(2.5) = 3 which is the middle of 5.
I hope this helps!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!