# Extracting sub elements in array

**URL:** <https://www.ruby-forum.com/t/extracting-sub-elements-in-array/85453>\
**Category:** Ruby\
**Created:** [February 13, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/extracting-sub-elements-in-array/85453 "2007-02-13T18:19:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wkc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3be4f8/32.png) [@wkc](https://www.ruby-forum.com/u/wkc)\
**Post date:** [February 13, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/extracting-sub-elements-in-array/85453/1 "2007-02-13T18:19:43Z")

</div>

Is there a fast way to return a subsection of an array object as there  
is the minor function for a Matrix?

For example  
testArray = [[1,2,3,4,5],  
[7,8,9,10,11]]

How can the elements [[4,5],[10,11]] be extracted?

Thanks,

---

<div class="post-metadata">

**Author:** ![wkc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3be4f8/32.png) [@wkc](https://www.ruby-forum.com/u/wkc)\
**Post date:** [February 13, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/extracting-sub-elements-in-array/85453/2 "2007-02-13T18:27:27Z")

</div>

On 2/13/07, WKC CCC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

testArray.map {|x| x[-2…-1]}

I don’t know if it is “fast”, but it was certainly quick to write 😉

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![wkc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3be4f8/32.png) [@wkc](https://www.ruby-forum.com/u/wkc)\
**Post date:** [February 13, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/extracting-sub-elements-in-array/85453/3 "2007-02-13T19:05:31Z")

</div>

- WKC CCC (removed\_email\_address@domain.invalid) wrote:

> Is there a fast way to return a subsection of an array object as there  
> is the minor function for a Matrix?
> 
> For example  
> testArray = [[1,2,3,4,5],  
> [7,8,9,10,11]]
> 
> How can the elements [[4,5],[10,11]] be extracted?

Use the built-in matrix class:

require ‘matrix’

m = Matrix.rows (1…5).to\_a, (7…11).to\_a

# extract the subsection at rows 0…1 and cols 3…4

m.minor(0…1, 3…4) #=\> Matrix[[4, 5], [10, 11]]

See the matrix documentation for additional documentation:

[http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html)

---

<div class="post-metadata">

**Author:** ![wkc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3be4f8/32.png) [@wkc](https://www.ruby-forum.com/u/wkc)\
**Post date:** [February 13, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/extracting-sub-elements-in-array/85453/4 "2007-02-13T19:23:32Z")

</div>

Thanks,

I’ve created the following function as an extention to class Array:

def Section(startRow,nRows,startCol,nCols)  
part = self[startRow…startRow+nRows-1]  
part = part.map{|x| x[startCol…startCol + nCols-1]}  
return part  
end

testArray=[[1,2,3,4,5],  
[6,7,8,9,10],  
[11,12,13,14],  
[15,16,17,18]]

calling testArray.Section(2,2,2,2)

will return [[13,14],[17,18]]

which is essentially what the minor function but on an Array

---

<div class="post-metadata">

**Author:** ![wkc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3be4f8/32.png) [@wkc](https://www.ruby-forum.com/u/wkc)\
**Post date:** [February 13, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/extracting-sub-elements-in-array/85453/5 "2007-02-13T20:27:42Z")

</div>

On 2/13/07, WKC CCC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> testArray=[[1,2,3,4,5],  
> [6,7,8,9,10],  
> [11,12,13,14],  
> [15,16,17,18]]
> 
> calling testArray.Section(2,2,2,2)
> 
> will return [[13,14],[17,18]]

def section( rows, cols )  
self.slice(rows).map! {|c| c.slice(cols)}  
end

testArray = [  
[1,2,3,4,5],  
[6,7,8,9,10],  
[11,12,13,14,15],  
[16,17,18,19,20]  
]

testArray.section( 2…4, 2…4 )

[[13, 14],  
[18, 19]]

Blessings,  
TwP
