Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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
Matrix 给定矩阵F中单元的返回邻域#_Matrix_F#_Neighbours - Fatal编程技术网

Matrix 给定矩阵F中单元的返回邻域#

Matrix 给定矩阵F中单元的返回邻域#,matrix,f#,neighbours,Matrix,F#,Neighbours,我写了一个小片段,用于在NxN矩阵中提取给定单元的任何邻居。像这样 let getNeighbours (x,y) (matrix: 'a [,]) = let lower n = max 0 (n - 1) let upper n = min (matrix.GetUpperBound(0)) (n + 1) matrix.[lower x..upper x, lower y..upper y] val arr : int [,] = [[29; 42; 0; 46

我写了一个小片段,用于在NxN矩阵中提取给定单元的任何邻居。像这样

let getNeighbours (x,y) (matrix: 'a [,]) = 
    let lower n = max 0 (n - 1)
    let upper n = min (matrix.GetUpperBound(0)) (n + 1)
    matrix.[lower x..upper x, lower y..upper y]

val arr : int [,] = [[29; 42; 0; 46; 55; 79; 18; 8]
                     [94; 25; 20; 45; 88; 73; 51; 69]
                     [62; 38; 66; 21; 55; 30; 37; 95]
                     [13; 35; 91; 0; 80; 15; 81; 22]
                     [2; 45; 94; 28; 50; 50; 35; 64]
                     [67; 98; 94; 63; 32; 11; 83; 23]
                     [38; 71; 31; 45; 52; 20; 20; 98]
                     [5; 4; 33; 19; 87; 17; 28; 78]]

> getNeighbours (4,0) arr;;
val it : int [,] = [[13; 35]
                    [2; 45]
                    [67; 98]]
现在它如预期的那样工作,我对F#Interactive显示2D阵列的方式有点不满意(它翻转阵列,使X轴垂直显示,而Y轴水平显示),但除此之外,没有任何抱怨


但是我不知道如何以简洁的方式将给定的单元格从相邻单元格中排除,假设矩阵中每个单元格的值都可以保持相同的值,因此,给定单元格的唯一唯一标识符将是它的索引。

我最终采用的方法是将数组展平,将其转换为映射,并删除给定单元格的第一个出现。它不像我希望的那样简洁或漂亮,也许其他人会有更好的解决方案

let getNeighbours (x,y) (matrix: 'a [,]) = 
    let flatten (arr: ((int * int) * 'a) [,]) = 
        arr |> Seq.cast<((int * int) * 'a)>
    let lower n = max 0 (n - 1)
    let upper n = min (matrix.GetUpperBound(0)) (n + 1)
    let hmap = matrix.[lower x..upper x, lower y..upper y] 
               |> Array2D.mapi (fun i j value -> ((i,j), value))
               |> flatten
               |> Map.ofSeq
    hmap.Remove (Map.findKey (fun key value -> value = matrix.[x, y]) hmap)
    |> Map.fold (fun acc _ value -> acc |> List.append [value]) []
让getneights(x,y)(矩阵:'a[,])=
让展平(arr:((int*int)*'a)[,]=

arr |>Seq.cast我最后的处理方法是将数组展平,将其转换为一个映射,并删除给定单元格的第一次出现。它不像我希望的那样简洁或漂亮,也许其他人会有更好的解决方案

let getNeighbours (x,y) (matrix: 'a [,]) = 
    let flatten (arr: ((int * int) * 'a) [,]) = 
        arr |> Seq.cast<((int * int) * 'a)>
    let lower n = max 0 (n - 1)
    let upper n = min (matrix.GetUpperBound(0)) (n + 1)
    let hmap = matrix.[lower x..upper x, lower y..upper y] 
               |> Array2D.mapi (fun i j value -> ((i,j), value))
               |> flatten
               |> Map.ofSeq
    hmap.Remove (Map.findKey (fun key value -> value = matrix.[x, y]) hmap)
    |> Map.fold (fun acc _ value -> acc |> List.append [value]) []
让getneights(x,y)(矩阵:'a[,])=
让展平(arr:((int*int)*'a)[,]=

arr |>Seq.cast根据您提出的解决方案,这里有另一种方法,但使用:

让getneights(x,y)(矩阵:'a[,])=
设下限n=最大值0(n-1)
设上限n=min(矩阵.GetUpperBound(0))(n+1)
序号{
对于i=下x到上x do
对于j=下y到上y do
如果(i,j)(x,y)那么
屈服矩阵[i,j]}

根据您提出的解决方案,以下是另一种方法:

让getneights(x,y)(矩阵:'a[,])=
设下限n=最大值0(n-1)
设上限n=min(矩阵.GetUpperBound(0))(n+1)
序号{
对于i=下x到上x do
对于j=下y到上y do
如果(i,j)(x,y)那么
屈服矩阵[i,j]}

我非常喜欢这个解决方案,很好!我非常喜欢这个解决方案,很好!