嵌套列表的平均值
Mean of Nested Lists
我正在尝试计算嵌套列表的平均值。我试过使用 map 函数,但默认值给出了我感兴趣的相反维度的平均值。请参见下面的示例:
set a [[1 1][2 2][3 3]] ; create a nested list
set b map mean a ; b equals [1 2 3]
此答案为 b 提供 [1 2 3]
。但是,我对 [2 2]
的答案很感兴趣,方法是在 "other" 维度中取平均值。我想有一种方法可以用 map
来做到这一点,但还没有想出来。
可能的解决方案如下
set a [[1 2 3] [1 2 3]]
set b map mean a
这会给你 [2 2]
b.
to go
print column-means [
[ 1 1 ]
[ 2 2 ]
[ 3 3 ]
]
end
to-report column-means [ matrix ]
if length (remove-duplicates map length matrix) > 1 [
error "All rows must be the same length"
]
report n-values length first matrix [ mean extract ? matrix ]
end
to-report extract [ i row ]
report map [ item i ? ] row
end
我正在尝试计算嵌套列表的平均值。我试过使用 map 函数,但默认值给出了我感兴趣的相反维度的平均值。请参见下面的示例:
set a [[1 1][2 2][3 3]] ; create a nested list
set b map mean a ; b equals [1 2 3]
此答案为 b 提供 [1 2 3]
。但是,我对 [2 2]
的答案很感兴趣,方法是在 "other" 维度中取平均值。我想有一种方法可以用 map
来做到这一点,但还没有想出来。
可能的解决方案如下
set a [[1 2 3] [1 2 3]]
set b map mean a
这会给你 [2 2]
b.
to go
print column-means [
[ 1 1 ]
[ 2 2 ]
[ 3 3 ]
]
end
to-report column-means [ matrix ]
if length (remove-duplicates map length matrix) > 1 [
error "All rows must be the same length"
]
report n-values length first matrix [ mean extract ? matrix ]
end
to-report extract [ i row ]
report map [ item i ? ] row
end