Access a specific row from an array
Access a specific row from an array
Is there any way I can access a specific row from an array, i.e., I have this array:
#2A((1 2) (3 4))
How can I get the first row as #A(1 2)?
Thanks in advance.
#2A((1 2) (3 4))
How can I get the first row as #A(1 2)?
Thanks in advance.
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: Access a specific row from an array
Unfortunately, I believe the only way to do this explicitly is to create a fresh vector and copy over the elements of that row. You can also do this implictly using closures:
And
And, if you want to be able to change the value
Code: Select all
(defun nth-row (array index)
(lambda (col)
(aref array index col)))
Code: Select all
(let* ((array #2A((1 2) (3 4)))
(row-0 (nth-row array 0)))
(dotimes (i 2)
(print (funcall row-0 i))))
Code: Select all
(defun nth-row (array index)
(lambda (col &optional new-val)
(if new-val
(setf (aref array index col) new-val)
(aref array index col))))
Re: Access a specific row from an array
See documentation on make-array, in particular :displaced-to and :displaced-index-offset, for example:
Note that due to how arrays are laid out in memory this can only be used to access rows or parts of rows, and not columns.
There is also affine indexing library, which might be used for similar purposes.
Code: Select all
CL-USER> (defparameter *a* (make-array (list 2 2) :initial-contents '((1 2)(3 4))))
*A*
CL-USER> (make-array 2 :displaced-to *a*)
#(1 2)
CL-USER> (make-array 2 :displaced-to *a* :displaced-index-offset 1)
#(2 3)
CL-USER> (make-array 2 :displaced-to *a* :displaced-index-offset 2)
#(3 4)
There is also affine indexing library, which might be used for similar purposes.
Re: Access a specific row from an array
Thanks, make-array's keyworded parameters solved my problem.


-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: Access a specific row from an array
Hum, my bad, I didn't think about this. Oftenly I use displaced arrays to turn multidimentional arrays into one-dimensional, or to make a destructive version of subseq.Ramarren wrote:See documentation on make-array, in particular :displaced-to and :displaced-index-offset, for example:Note that due to how arrays are laid out in memory this can only be used to access rows or parts of rows, and not columns.Code: Select all
CL-USER> (defparameter *a* (make-array (list 2 2) :initial-contents '((1 2)(3 4)))) *A* CL-USER> (make-array 2 :displaced-to *a*) #(1 2) CL-USER> (make-array 2 :displaced-to *a* :displaced-index-offset 1) #(2 3) CL-USER> (make-array 2 :displaced-to *a* :displaced-index-offset 2) #(3 4)
There is also affine indexing library, which might be used for similar purposes.
But this doesn't work with columns, and my version can be easily addapted
