What is a multi-dimensional array

In Ruby, a multi-dimensional array is an array that contains other arrays as its values or members. The container array is termed an outer array, and the member array is termed an inner array. An array is said to be a nested array if it has another array as one or more of its members, even if all other members of the outer array are not arrays.

The following program illustrates the structure of a multi-dimensional array:

Press + to interact
alist = ['aa', ['b', 'c'], 'dd'] # Creating a multi-dimensional array
print(alist)

The following illustration shows the structure of the array above:

And here’s another example of the structure of a multi-dimensional array:

Press + to interact
blist = ['0', '1', ['2.0', '2.1', ['2.2.0', '2.2.1']], '3', '4'] # Creating a multi-dimensional array
print(blist)

The following illustration shows the structure of the array above:

And here’s another example of a multi-dimensional array:

Press + to interact
clist = [[1, 2, 3], [4, 5], [5], 77 , 8.8, 'ff'] # Creating a multi-dimensional array
print(clist)

The following illustration shows the structure of the above array.

We can also create a new array, dlist, with the help of all the lists above (alist, blist, and clist) using array initialization.

Press + to interact
alist = ['aa', ['b', 'c'], 'dd'] # Creating array 1
blist = ['0', '1', ['2.0', '2.1', ['2.2.0', '2.2.1']], '3', '4'] # Creating array 2
clist = [[1, 2, 3], [4, 5], [5], 77, 8.8, 'ff'] # Creating array 3
dlist = [alist, blist, clist] # Creating array of arrays
print(dlist)

In the code above:

  • We create three arrays—alist, blist, and clist.
  • We create a new list, dlist, with the help of the other arrays using array initialization.

A mathematical matrix is implemented as a two-dimensional array in Ruby, as shown in the following example:

matrixA=[123456789101112] matrixA = \begin{bmatrix} 1&2&3&4 \\ 5&6&7&8 \\ 9&10&11&12 \end{bmatrix}

The general concepts of two-dimensional arrays, n-dimensional arrays, or jagged arrays are implemented as multi-dimensional arrays in Ruby. The following program illustrates the structure of a two-dimensional array:

Press + to interact
matrixA = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # Creating a matrix
print("Matrix: ",matrixA)

The variable matrixA is an array of three elements, and each element is itself an array of four elements.

The following code demonstrates a way to write values in the forms of rows and columns:

Press + to interact
matrixB = [ [10,12,13,14],
[15,16,17,18],
[19,20,21,22] ]
print("Matrix: ",matrixB)

The result of the second program is exactly the same as the first program. Inside the computer’s memory, both variables (matrixA and matrixB) are stored in the same way—as linear sequences. Now, say we want to concatenate these two matrices into another matrix (matrixC).

Press + to interact
matrixA = [[1, 2, 3, 4], [5, 6, 7, 8],[9, 10, 11, 12]]
matrixB = [ [10, 12, 13, 14],
[15, 16, 17, 18],
[19, 20, 21, 22] ]
matrixC = matrixA + matrixB # Adding matrix A and matrix B
print("Matrix A: ",matrixA,"\n")
print("Matrix B: ",matrixB,"\n")
print("Matrix C: ",matrixC,"\n")

The resulting matrix isn’t a sum of values of operands. Rather, it’s a matrix that has six members (three from matrixA and three from matrixB). We need to access the individual integer values in each matrix to produce their sum.

Individual values in multi-dimensional array

The individual values in a multi-dimensional array are accessed through multiple index numbers used in a row. Let’s take the example of the following array:

array1 = [0, 1, [20, 21, [220, 221]], 3, 4]
  • array1 is a multi-dimensional array.
  • The value 20 can be accessed as array1 [ 2 ] [ 0 ], and the value 221 can be accessed as array1 [2] [2] [1].
  • The values which are not nested in array1 can be accessed with a single index number. For example, array1[4] is 4.
  • The number of indexes depends on the level of nesting.

The following illustration shows the structure of the array above. The main level elements are array1[0] , array1[1], array1[3], and array1[4]. The element array1[2] has another subarray, and the elements of the subarray can be accessed as array1[2][0] and array1[2][1]. The last element array1[2][2] has another subarray, and the elements can be accessed as array1[2][2][0] and array1[2][2][1].

The following program calculates the sum of two matrix variables:

Press + to interact
a = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]]
b = [ [1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12] ]
c = [[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0]] #Creating a matrix c of zeros
print("Matrix A: ",a,"\n")
print("Matrix B: ",b,"\n")
rows,cols = 3,4
for i in 0...rows # Outer loop for rows
for j in 0...cols # Inner loop for columns
c[i][j] = a[i][j] + b[i][j] # Adding values of matrix a and b and assigning the result in matrix c
end
end
print("Matrix Sum: ",c,"\n")

In the program above:

  • We declare and initialize two matrices, a and b.
  • We define another matrix c and initialize it with zeros.
  • We traverse the individual values of a and b to add in the corresponding values of c.
  • The variable rows is 3, which represents the number of rows of the matrix.
  • The variable cols is 4, which represents the number of columns of the matrix.

Practice multi-dimensional array

The following are a few example programs to practice using multi-dimensional arrays in Ruby. By clicking the “Show Solution” button, you can find a program that solves the respective problem. You can copy and paste the given solution into the code widget to make sure the output of your solution matches the given solution. There may be several ways of writing correct solutions in programming.

Calculate the sum of each row in a matrix

Write a program to show the sum of each row of the matrix. The matrix must have two rows and three columns.

Sample input 1

[[10,20,30],
  [40,50,60]]

Sample output 1

Matrix: [[10, 20, 30], [40, 50, 60]]
Sum of Row1: 60
Sum of Row2: 150

Sample input 2

[[1,2,3],
  [4,5,6]]

Sample output 2

Matrix: [[1, 2, 3], [4, 5, 6]]
Sum of Row1: 6
Sum of Row2: 15
Press + to interact
# Write your code here

Calculate the sum of each column in a matrix

Write a program to show a matrix in the form of rows and columns, then show the sum of each column. The matrix must have five rows and ten columns.

Sample input

[[0,1,2,3,4,5,6,7,8,9],
[10,11,12,13,14,15,16,17,18,19],
[20,21,22,23,24,25,26,27,28,29],
[30,31,32,33,34,35,36,37,38,39],
[40,41,42,43,44,45,46,47,48,49]]

Sample output

Displaying in the matrix form:

row# 0    ==>    0	1	2	3	4	5	6	7	8	9	
row# 1    ==>    10	11	12	13	14	15	16	17	18	19	
row# 2    ==>    20	21	22	23	24	25	26	27	28	29	
row# 3    ==>    30	31	32	33	34	35	36	37	38	39	
row# 4    ==>    40	41	42	43	44	45	46	47	48	49	

Column Sums are: [100, 105, 110, 115, 120, 125, 130, 135, 140, 145]
Press + to interact
# Write your code here