MATLAB Answers

0

Taking middle 4 values of n size array

Asked by James Morris on 20 Aug 2019 at 19:49
Latest activity Commented on by James Morris on 23 Aug 2019 at 21:41
I know how to take the first 4 values of an n size array (1:4)
and the last 4 (end-4:end)
but how would I take the middle 4 values of an n size array no matter the length?
Thanks

  1 Comment

the cyclist
on 20 Aug 2019 at 19:55
Do you know if the number of array elements is always even, or always odd? How do you want to define the middle if there are an odd number?

Sign in to comment.

3 Answers

Answer by Jon
on 20 Aug 2019 at 19:56
Edited by Jon
on 20 Aug 2019 at 19:59
 Accepted Answer

You could use something like, for an array A
n = length(A(:)) % : treats as column even if this is actually a multidimensional array
midpoint = round(n/2)
select = A(midpoint-1:midpoint+2); % slightly unsymmetrical 1 before, 2 after
Note, you may have to further clarify, what your really mean by "middle 4". There are two sources of asymmetry inherent in taking the "middle 4". If there are an even number of elements then the "middle" one is either closer to one end or the other. Also as you are taking an even number (4) elements then either they are offset one way or the other from the selected middle element

  4 Comments

Show 1 older comment
Jon
on 21 Aug 2019 at 12:54
I agree, middle four for n even is well defined. For n odd you end up with it biased one to the left or or one to the right depending how you choose.
Here is a simple way of doing it which will be symmetrical for n even, and biased to the left for n odd
n = length(A(:)) % or you could use n = numel(A) if you prefer that style
midpoint = floor(n/2)
select = A(midpoint-1:midpoint+2);
or if you want it biased to the right you can use
n = length(A(:)) % or you could use n = numel(A) if you prefer that style
midpoint = ceil(n/2)
select = A(midpoint-1:midpoint+2);
Note that in my original solution I used round instead of floor or ceil. Since the rounding rules round up for values where the decimal part is greater than or equal to 0.5 round will give the same result as ceil (bias to the right) . Using ceil makes what is happening clearer.
Also my comment in the original solution, was not really clear. It is infact symmetrical for n even.
Jon
on 23 Aug 2019 at 14:37
Are you still looking for more help with this?
Sorry for the late reply. No I figured it out in the end. You guys are awesome, thank you.

Sign in to comment.


Answer by the cyclist
on 20 Aug 2019 at 19:58
Edited by the cyclist
on 20 Aug 2019 at 20:13

Here is one way, assuming the number of elements is even:
% Input
a = rand(1,8);
numberElements = numel(a);
numberToRemove = numberElements - 4;
numberToRemoveFromEachEnd = numberToRemove/2;
output = a(numberToRemoveFromEachEnd+1:end-numberToRemoveFromEachEnd)

  0 Comments

Sign in to comment.


Answer by the cyclist
on 20 Aug 2019 at 20:09
Edited by the cyclist
on 20 Aug 2019 at 20:13

Here is one way, which will work for either even- or odd-length arrays. It is not efficient.
% Input
a = rand(1,9);
whichEnd = 1;
while numel(a) > 4;
a = a(whichEnd:(end+whichEnd-2))
whichEnd = 3-whichEnd;
end
end
It will choose the "slightly left of center" for odd-length array.

  0 Comments

Sign in to comment.