我試圖找到一種方法來創建切換行和列的數組副本。
我想通過設置陰影數組指向原始數組的指針,使item [2 1]指向item [1 2]。創建具有切換行和列的數組的陰影陣列
使用指針會產生這樣的效果,即在更改原始數組時,陰影數組也會反映該更改。
到目前爲止,我想出了陣列中每個項目都使用長度爲1的位移數組。
它的作品,但結果是陣列的數組。
我的問題是,有沒有更好的方法來做到這一點?
是否有一種方法可以通過陰影數組使用setf來設置原始數組中的值?
我迄今爲止代碼:
(defun shadow-column-array (array)
(let* ((colsize (second (array-dimensions array)))
(rowsize (first (array-dimensions array)))
(b (make-array (list colsize rowsize))))
(dotimes (i rowsize b)
(dotimes (j colsize)
(setf (aref b j i)
(make-array 1
:displaced-to array
:displaced-index-offset (+ (* i rowsize) j)))))))
一些輸出:
*a* => #2A((0.0 0.1 0.2) (1.0 1.1 1.2) (2.0 2.1 2.2))
(setq b (shadow-column-array (*a*))
*b* => #2A((#(0.0) #(1.0) #(2.0)) (#(0.1) #(1.1) #(2.1)) (#(0.2) #(1.2) #(2.2)))
流離失所列的陣列現在
col1 => #(#(0.0) #(1.0) #(2.0))
col2 => #(#(0.1) #(1.1) #(2.1))
col3 => #(#(0.2) #(1.2) #(2.2))