我定义了一个矩阵,所以如果我这样做
(format t "~a" (get-real-2d 0 0))
它打印出第一行第一列中的元素
如果我这样做
(format t "~a" (get-real-2d a 0 1))
它打印出第一行第二列中的元素
如果我这样做
(format t "~a" (get-real-2d a 1 0))
它打印出第二行第一列中的元素。
矩阵a
看起来像这样
a =
((0 1 2)
(3 4 5)
(6 7 8))
我希望你能准确地告诉我如何编写一个dotimes
循环或其他循环,以尽可能少的行将使用该get-real-2d
函数打印出矩阵,因此输出如下所示:
0 1 2
3 4 5
6 7 8
我只是希望你能给我展示一个非常小的光滑循环,我可以用它来打印我可以在我的 lisp 库中使用的矩阵,看起来很专业,比如只使用变量的。就像是:
(format t "~a" (get-real-2d i j))
而不是一堆:
(format t "~a" (get-real-2d 0 0))
(format t "~a" (get-real-2d 0 1))
(format t "~a" (get-real-2d 0 2))
;;;;最新编辑;;; 为了简单起见,我打电话给
(defparameter a (create-mat 3 3 +32fc1+))
创建一个 3x3 矩阵 - create-mat 是 opencv 的 cvCreateMat 的包装器
该命令在 repl 的输出是
(defparameter a (create-mat 3 3 +32fc1+))
A
CL-OPENCV> a
#.(SB-SYS:INT-SAP #X7FFFD8000E00)
i/e 变量 a 是指向 3x3 矩阵的指针
然后我跑
(defparameter data (cffi:foreign-alloc :float :initial-contents
'(0.0f0 1.0f0 2.0f0 3.0f0 4.0f0 5.0f0 6.0f0 7.0f0 8.0f0)))
为矩阵创建数据 - 我接下来将分配给矩阵
该命令在 repl 的输出是
CL-OPENCV> (defparameter data (cffi:foreign-alloc :float :initial-contents
'(0.0f0 1.0f0 2.0f0 3.0f0 4.0f0 5.0f0 6.0f0 7.0f0 8.0f0)))
DATA
CL-OPENCV> data
#.(SB-SYS:INT-SAP #X7FFFD8000E40)
i/e 变量 a 是指向数据的数据指针,将添加到矩阵
然后我打电话..
(set-data a data 12) to add the data to the matrix - set-data is a wrapper for opencv's cvSetData
所以现在当我运行时 - (get-real-2d 是 opencv 的 cvGetReal2d 的包装器)
(get-real-2d a 0 0) it gets the element of matrix a at row 0 col 0 which is 0.0d0
该命令在 repl 的输出是
CL-OPENCV> (get-real-2d a 0 0)
0.0d0
现在当我跑步时
(get-real-2d a 0 1) it gets the element of matrix a at row 0 col 1 which is is 0.0d0
该命令在 repl 的输出是
CL-OPENCV> (get-real-2d a 0 1)
1.0d0
当我运行这个循环时
(dotimes (i 3)
(dotimes (j 3)
(format t "~a~%" (get-real-2d a i j))))
该命令在 repl 的输出是
CL-OPENCV> (dotimes (i 3)
(dotimes (j 3)
(format t "~a~%" (get-real-2d a i j))))
0.0d0
1.0d0
2.0d0
3.0d0
4.0d0
5.0d0
6.0d0
7.0d0
8.0d0
NIL
但是当我尝试你的方法@Svante
(dotimes (i 3)
(dotimes (j 3)
(format t "~{~{~a~^ ~}~%~}" (get-real-2d a i j))))
我得到错误:
The value 0.0d0 is not of type LIST.
[Condition of type TYPE-ERROR]
因为 1 次运行 get-real-2d 的输出只是 1 个浮点数 i/e
CL-OPENCV> (get-real-2d a 0 0)
0.0d0
有了这些信息,你能帮我打印矩阵吗,看起来像这样
0.0d0 1.0d0 2.0d0
3.0d0 4.0d0 5.0d0
6.0d0 7.0d0 8.0d0