Extracting sub elements in array

W

WKC CCC

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,
 
T

Tim Pease

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,

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

I don't know if it is "fast", but it was certainly quick to write ;)

Blessings,
TwP
 
P

Paul Duncan

--rJwd6BRFiFCcLxzm
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* WKC CCC ([email protected]) said:
Is there a fast way to return a subsection of an array object as there
is the minor function for a Matrix?
=20
For example
testArray =3D [[1,2,3,4,5],
[7,8,9,10,11]]
=20
How can the elements [[4,5],[10,11]] be extracted?

Use the built-in matrix class:

require 'matrix'

m =3D 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) #=3D> Matrix[[4, 5], [10, 11]]

See the matrix documentation for additional documentation:
=20
http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html

--=20
Paul Duncan <[email protected]> pabs in #ruby-lang (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

--rJwd6BRFiFCcLxzm
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFF0f3PzdlT34LClWIRAi1dAJ9oThOpiWNd3je+zOXPzSVGF9TxpwCgwcX5
lu/vhbolh/ydwKCN4m/1JqI=
=7b1W
-----END PGP SIGNATURE-----

--rJwd6BRFiFCcLxzm--
 
W

WKC CCC

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
 
T

Tim Pease

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]]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top