0

我试图用零填充一个数组,但找不到一种简洁的方法来快速执行此操作。如果我有数组 x = [1,2,3 ; 4,5,6; 7,8,9] 在matlab中我可以使用命令

y = [zeros(1,3+2);zeros(3,1),x,zeros(3,1);zeros(1,3+2)]

给出所需的输出数组 [0,0,0,0,0; 0,1,2,3,0 ; 0,4,5,6,0 ; 0,7,8,9,0 ; 0,0,0,0,0]

然而到目前为止,在一个快速的操场上,我只能单独引用每个元素以正确形成新数组。

到目前为止我尝试过的方法使用 x 作为输入和 y 作为输出,第一个类似于 matlab,

var x = [[1,2,3,],[4,5,6],[7,8,9]]

var y = [[0,0,0,0,0],[0,x[0],0],[0,x[1],0],[0,x[2],0],[0,0,0,0,0]]

第二个是循环

for i in 0 ..< x.count + 1 {
  if i == 0 || i == x.count - 1 {
    y[i] = [0,0,0,0,0]
  }
  else{
    y[i] = [0, x[i-1] ,0]
  }
}

这是给定的输出,而不是看起来像 Xcode 游乐场预览中的标准数组。

[[0, 0, 0, 0, 0], [0, […], 0], [0, 0, 0, 0, 0], [0, […], 0], [0, 0, 0, 0, 0]]

使用代码也很奇怪地打印到控制台

for i in 0 ..< y.count {
    print("\(y[i])")
}

输出是

(
    0,
    0,
    0,
    0,
    0
)
(
    0,
        (
        1,
        2,
        3
    ),
    0
)
(
    0,
    0,
    0,
    0,
    0
)
(
    0,
        (
        7,
        8,
        9
    ),
    0
)
(
    0,
    0,
    0,
    0,
    0
)

与预期相反

[0, 0, 0, 0, 0]
[0, 1, 2, 3, 0]
[0, 4, 5, 6, 0]
[0, 7, 8, 9, 0]
[0, 0, 0, 0, 0]

做这个的最好方式是什么?

4

3 回答 3

2

我制作了appzYourLife 答案的通用版本,可以采用任意嵌套数组类型。它还增加了对顶部和底部填充的支持

extension Array where Element: _ArrayType {
    typealias InnerElement = Element.Generator.Element

    func pad2DArray(with padding: InnerElement,
                    top: Int = 0, left: Int = 0,
                    right: Int = 0, bottom: Int = 0) -> [[InnerElement]] {
        let newHeight = self.count + top + bottom
        let newWidth = (self.first?.count ?? 0) + left + right

        var paddedArray = [[InnerElement]](count: newHeight, repeatedValue:
                        [InnerElement](count: newWidth, repeatedValue: padding))

        for (rowIndex, row) in self.enumerate() {
            for (columnIndex, element) in row.enumerate() {
                paddedArray[rowIndex + top][columnIndex + left] = element
            }
        }

        return paddedArray
    }
}

var input = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

let result = input.pad2DArray(with: 0, top: 1, left: 1, right: 1, bottom: 1)

/*
result:
[
    [0, 0, 0, 0, 0],
    [0, 1, 2, 3, 0],
    [0, 4, 5, 6, 0],
    [0, 7, 8, 9, 0],
    [0, 0, 0, 0, 0],
]
*/
于 2016-08-05T03:07:55.577 回答
-1

绝对没有公认的答案那么优雅,但它仍然有效:

var x = [[1,2,3,],[4,5,6],[7,8,9]]
var y = [[Int]]()

y.insert([0,0,0,0,0], atIndex: 0)

for i in 0 ..< x.count {
    var intArray: [Int] = []
    for number in x[i] {
        intArray.append(number)
    }
    intArray.insert(0, atIndex: 0)
    intArray.insert(0, atIndex: intArray.count)
    y.append(intArray)
}

y.insert([0,0,0,0,0], atIndex: x.count + 1)

从输出

for i in 0 ..< y.count {
    print("\(y[i])")
}

[0, 0, 0, 0, 0]

[0, 1, 2, 3, 0]

[0, 4, 5, 6, 0]

[0, 7, 8, 9, 0]

[0, 0, 0, 0, 0]

于 2016-08-04T22:18:28.273 回答
-1

扩大

如果你定义这个扩展

extension _ArrayType where Element == Int {
    func pad(left left: Int, right: Int) -> [Int] {
        let leftSide = [Int](count: left, repeatedValue: 0)
        let rightSide = [Int](count: right, repeatedValue: 0)
        return leftSide + (self as! [Int]) + rightSide
    }
}

然后你可以写

[1,2,3].pad(left: 1, right: 1) // [0, 1, 2, 3, 0]
于 2016-08-04T21:24:55.260 回答