f# - 在 F# 中切片二维数组但类似于 Matlab?

标签 f# f#-interactive f#-3.0 c#-to-f# f#-data

我想知道是否有一种方法可以使用列表或 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/

相关文章:

.net - 如何在 dotnet 核心中运行 F# interactive (fsi.exe)?现在还支持吗?

f# - 如何在我自己的代码中生成类似于 FSI 的 F# 类型签名?

用于转换多个不同度量单位的 F# 运算符重载

F# 类型提供程序和 REST api

f# - F# 3.0 中的事件模式被破坏

generics - F# 通用结构构造函数和错误 FS0670

f# - 静态成员索引属性

dictionary - f# 字典、不变性和性能

.net - F# 系统.TypeLoadException : The generic type 'System.Tuple` 3' was used with an invalid instantiation in assembly

macos - Mac 上的 F# Interactive 在哪里