我想知道是否有一种方法可以使用列表或 int 数组作为索引来切入数组以获得 f# 中的子数组。
我知道你可以做到以下几点
Arr2d.[*,1] or Arr2d.[1..5,1..2] etc...
但我一直在寻找类似 Matlab 的东西,你可以在其中编写:
Arr2d([1;6;10],1) or Arr2d(1:10,[1;6;10])
是否可以像 F# 中那样进行切片?
谢谢!
这是我的示例解决方案:(可能不是最优但有效)
let sampleMatrix = Array2D.init 10 5 (fun x y -> y)
val sampleMatrix : int [,] = [[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]]
let idxlist = [1;3;4]
let array2dColumnSlice (idxlist:list<int>) (data:'T[,]) =
let tmp = [|for i in idxlist -> data.[*,i]|]
Array2D.init tmp.[0].Length tmp.Length (fun x y -> tmp.[y].[x] )
let slice = array2dColumnSlice idxlist sampleMatrix
val slice : int [,] = [[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]]
最佳答案
详细here , 除了您已经发现的之外,没有其他切片符号。
关于f# - 在 F# 中切片二维数组但类似于 Matlab?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361951/