Swift matrix sum

前端 未结 3 1612
小蘑菇
小蘑菇 2021-01-16 08:18

I\'m trying to develop a func which allows to sum two matrix if they are equals to the same dimension, but I get an error \"EXC_BAD_INSTRUCTION\" on the try.

Here is

3条回答
  •  孤独总比滥情好
    2021-01-16 09:12

    Actually your error is Index out of range

    Replace your extension with this code.

    extension Matrix: Operation {
    
        mutating func sumWith(matrixB: Matrix) throws -> Matrix {
    
            guard self == matrixB else { throw RisedError.DimensionNotEquals }
    
            for row in 0...self.rows - 1 {
                for column in 0...self.columns - 1 {
                    self[row, column] = matrixB[row, column] + self[row, column]
                }
             }
            return self
        }
    }
    

    Hope it would solve your problem.

提交回复
热议问题