Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在CL中的dotimes循环中使用aref?_Arrays_Loops_Lisp_Common Lisp - Fatal编程技术网

Arrays 在CL中的dotimes循环中使用aref?

Arrays 在CL中的dotimes循环中使用aref?,arrays,loops,lisp,common-lisp,Arrays,Loops,Lisp,Common Lisp,我有一个维度为“(3 2)的数组,名称为test array,如下所示: #3A(((0 0) (0 0.1) (0 0.3)) ((1 0) (1 0.1) (1 0.3)) ((2 0) (2 0.1) (2 0.3))) 请注意,数组中最内部的列表(秩2)是点坐标,因此通常数组是 #3A(((x0 y0) (x0 y1) (x0 y2)) ((x1 y0) (x1 y1) (x1 y2)) ((x2 y0) (x2 y1) (x2 y2))) 现在我

我有一个维度为“(3 2)的数组,名称为test array,如下所示:

#3A(((0 0) (0 0.1) (0 0.3)) 
    ((1 0) (1 0.1) (1 0.3))
    ((2 0) (2 0.1) (2 0.3)))
请注意,数组中最内部的列表(秩2)是点坐标,因此通常数组是

#3A(((x0 y0) (x0 y1) (x0 y2)) 
    ((x1 y0) (x1 y1) (x1 y2))
    ((x2 y0) (x2 y1) (x2 y2)))
现在我想使用这个数组中的元素来创建一个新数组

在这个数组的每一行(数组秩:0),我想从第一个xy坐标中减去第二个xy坐标,从第二个xy坐标中减去第三个xy坐标。基本上,我在寻找参数形式的结果是:

#3A(((x0-x0 y1-y0) (x0-x0 y2-y1)) 
    ((x1-x1 y1-y0) (x1-x1 y2-y1))
    ((x2-x2 y1-y0) (x2-x2 y2-y1))
是否有任何简单的直接函数或操作仅通过操纵初始数组来实现这一点

因为我不知道这方面的任何直接操作,所以我想从初始数组中创建一个列表,作为新数组的
:initial contents
。因此,该方法的初始目标是获得初始内容列表,即:

   (((x0-x0 y1-y0) (x0-x0 y2-y1)) 
    ((x1-x1 y1-y0) (x1-x1 y2-y1))
    ((x2-x2 y1-y0) (x2-x2 y2-y1))
为此,我想到了一个使用
dotimes
两次的代码(外部循环的数量就是行的数量,内部循环的数量就是列的数量):

但有一个问题,这也是问题的标题。显然,CL:
(aref测试数组n m 0)
不理解这种为
aref
提供输入的“参数化”方式(使用n&m)

为什么会有这样的问题?你能想出其他方法在循环中使用aref,或者用另一种方法制作:初始内容列表吗

注意,这是我实际问题的一个相对简单的形式,因为我的实际初始数组的维数为(21162),所有的xy坐标彼此不同


非常感谢您的回答…

正如我在评论中注意到的,函数返回其参数 递增1是
1+
,而不是
+1
(它不是函数名,而是 常数
1

我认为没有必要在这里构建
:初始内容
,因为它没有
使事情比构建目标数组并填充它更容易
在循环中作为一个单独的步骤。我会这样做:

(defparameter *test-array*
  #3A(((0 0) (0 0.1) (0 0.3)) 
      ((1 0) (1 0.1) (1 0.3))
      ((2 0) (2 0.1) (2 0.3))))

(destructuring-bind (rows cols axes) (array-dimensions *test-array*)
  (let ((result (make-array
                 (list rows (1- cols) axes)
                 :element-type (array-element-type *test-array*))))
    (dotimes (row rows result)
      (dotimes (col (1- cols))
        (dotimes (axis axes)
          (setf (aref result row col axis)
                (- (aref *test-array* row (1+ col) axis)
                   (aref *test-array* row col axis))))))))
我决定让这个例子尽可能简单。可供替代的 解决方案是可能的,例如,利用移动到 数组(这就是处理多维数组片段的方式 使用序列函数)。我更喜欢简单的方法
如果不是更复杂的情况,则此任务。

如果您使用
1+
(这是一个函数名)而不是
+1
(这是编写
1
的另一种方式),CL将非常感激。@AntonKovalenko是的,刚刚注意到并更改了它。那是打字错误!此更改将使“问题中的主要问题”无效,因此您可能需要相应地对其进行编辑。尽管如此,我还是试图回答问题的其余部分。
(defparameter *test-array*
  #3A(((0 0) (0 0.1) (0 0.3)) 
      ((1 0) (1 0.1) (1 0.3))
      ((2 0) (2 0.1) (2 0.3))))

(destructuring-bind (rows cols axes) (array-dimensions *test-array*)
  (let ((result (make-array
                 (list rows (1- cols) axes)
                 :element-type (array-element-type *test-array*))))
    (dotimes (row rows result)
      (dotimes (col (1- cols))
        (dotimes (axis axes)
          (setf (aref result row col axis)
                (- (aref *test-array* row (1+ col) axis)
                   (aref *test-array* row col axis))))))))