Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Arrays 用0'填充数组;斯威夫特_Arrays_Swift_Pad - Fatal编程技术网

Arrays 用0'填充数组;斯威夫特

Arrays 用0'填充数组;斯威夫特,arrays,swift,pad,Arrays,Swift,Pad,我试图用零填充数组,但在swift中找不到一种简洁的方法。 如果我有数组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] 然而,到目前为止,在swift游乐场中,我只能单独引用每个元素以正确地形成新数组 到目前为止,我已经尝试过使用x作

我试图用零填充数组,但在swift中找不到一种简洁的方法。 如果我有数组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]

然而,到目前为止,在swift游乐场中,我只能单独引用每个元素以正确地形成新数组

到目前为止,我已经尝试过使用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]
  }
}
它还使用代码以非常奇怪的方式打印到控制台

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]
最好的方法是什么?

扩展 如果定义此扩展名

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]
延伸 如果定义此扩展名

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]

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

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)
var x=[[1,2,3,],[4,5,6],[7,8,9]]
变量y=[[Int]]()
y、 插入([0,0,0,0,0],a索引:0)
对于0中的i..
来自

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

[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,0]


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

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)
var x=[[1,2,3,],[4,5,6],[7,8,9]]
变量y=[[Int]]()
y、 插入([0,0,0,0,0],a索引:0)
对于0中的i..
来自

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

[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,0]


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

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],
]
*/

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

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],
]
*/


循环,就像一个普通人一样。你可以提供输入和预期的输出吗?还有,你真的尝试过什么吗?另外,你的样本数组不匹配matlab命令(3x3 vs 3x5)给定你的matlab数组,
[1,2,3;4,5,6;7,8,9]
,相同的Swift数组应该是
[[1,2,3],[4,5,6],[7,8,9]
。您希望从中得到什么样的输出,您希望如何精确地填充零?请回答您的问题以添加信息s.loops,就像普通人一样。您可以提供输入和预期输出吗?此外,您实际尝试过什么吗?此外,您的示例数组与matlab命令(3x3 vs 3x5)不匹配。给定您的matlab数组,
[1,2,3;4,5,6;7,8,9]
,相同的Swift数组将是
[[1,2,3],[4,5,6],[7,8,9]
。您希望从中得到什么样的输出,您希望如何精确地填充零?请您的问题添加信息。@Hamish查看我的编辑:
(self.first?.count??0)
看起来不错–希望您不介意,但我冒昧地删除了多余的一致性和剩余的
print
statement:)我还删除了我的第一条评论,因为它们不再相关了。@Hamish谢谢,我忘记了those@Hamish看看我的编辑:
(self.first?count?0)
看起来不错–希望您不介意,但我冒昧地删除了多余的一致性和剩余的
print
statement:)我还删除了我的第一条评论,因为它们不再相关了。@Hamish谢谢,我忘记了those@AlexanderMomchliov:看起来不错;)@AlexanderMomchliov:看起来不错;)