6 kyu

Matrix Addition

4,286 of 13,989xDranik

Description:

Write a function that accepts two square matrices (N x N two dimensional arrays), and return the sum of the two. Both matrices being passed into the function will be of size N x N (square), containing only integers.

How to sum two matrices:

Take each cell [n][m] from the first matrix, and add it with the same [n][m] cell from the second matrix. This will be cell [n][m] of the solution matrix. (Except for C where solution matrix will be a 1d pseudo-multidimensional array).

Visualization:

|1 2 3|     |2 2 1|     |1+2 2+2 3+1|     |3 4 4|
|3 2 1|  +  |3 2 3|  =  |3+3 2+2 1+3|  =  |6 4 4|
|1 1 1|     |1 1 3|     |1+1 1+1 1+3|     |2 2 4|

Example

matrixAddition(
  [ [1, 2, 3],
    [3, 2, 1],
    [1, 1, 1] ],
//      +
  [ [2, 2, 1],
    [3, 2, 3],
    [1, 1, 3] ] )

// returns:
  [ [3, 4, 4],
    [6, 4, 4],
    [2, 2, 4] ]
Matrix
Arrays
Algorithms

Stats:

CreatedOct 19, 2013
PublishedOct 19, 2013
Warriors Trained21463
Total Skips2327
Total Code Submissions32745
Total Times Completed13989
JavaScript Completions4286
CoffeeScript Completions134
Ruby Completions931
C# Completions658
PHP Completions462
Python Completions3458
C++ Completions1300
Java Completions2227
C Completions520
Elixir Completions108
Lua Completions130
NASM Completions11
Rust Completions94
Scala Completions16
TypeScript Completions176
Total Stars257
% of votes with a positive feedback rating92% of 1521
Total "Very Satisfied" Votes1305
Total "Somewhat Satisfied" Votes185
Total "Not Satisfied" Votes31
Ad
Contributors
  • xDranik Avatar
  • jhoffner Avatar
  • SagePtr Avatar
  • anter69 Avatar
  • stevehopkinson Avatar
  • donaldsebleung Avatar
  • docgunthrop Avatar
  • Voile Avatar
  • Souzooka Avatar
  • rowcased Avatar
  • FArekkusu Avatar
  • sazzadshopno Avatar
  • hobovsky Avatar
  • uniapi Avatar
  • xavierguihot Avatar
  • trashy_incel Avatar
  • username0 Avatar
  • ejini战神 Avatar
  • MobulaKuhlii Avatar
  • akar-0 Avatar
  • skaarj1989 Avatar
  • Mednoob Avatar
  • mouthbreather Avatar
  • KayleighWasTaken Avatar
Ad