Getting the central n numbers in array..

6 vues (au cours des 30 derniers jours)
Anand Anand
Anand Anand le 12 Juil 2011
Is there an easy way to get central n numbers in an array?for eg x=[ 1 2 3 4 5] I want to extract the central 3 numbers (i.e) 2,3 and 4.Which is the shortest way to do that?

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Juil 2011
To extract the middle N elements of the vector x:
x((end-N)/2+1:(end+N)/2)
Warning: this will cause an error if you ask for an odd number of elements from a vector of even length, or an even number of elements from a vector of odd length.

Plus de réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by