-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays_matrix.bash
More file actions
109 lines (90 loc) · 1.58 KB
/
arrays_matrix.bash
File metadata and controls
109 lines (90 loc) · 1.58 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# ARRAYS
# Array definition in bash
my_array=(1 2 3 4 5)
# Another way to define an array in bash
my_array=([0]=1 [1]=2 [2]=3 [3]=4 [4]=5)
# Accesing a single element
echo ${my_array[0]}
# Accessing all elements
echo ${my_array[@]}
# Adding a new element
my_array+=(6)
# Iterating using a for loop
for element in "${my_array[@]}"; do
echo $element
done
# Array length
echo ${#my_array[@]}
# MATRIX
# Using an associative array
declare -A matrix
matrix[1]=1 2 3 4 5
# Or
matrix[0,0]=1
matrix[0,1]=2
matrix[1,0]=3
matrix[1,1]=4
# Accessing elements
echo ${matrix[0,0]} # Outputs: 1
echo ${matrix[1,1]} # Outputs: 4
# Define a 2x2 matrix
matrix=(
1 2
3 4
)
# Accessing elements (manual calculation of indices)
rows=2
cols=2
# Access element at row 1, column 0
row=1
col=0
index=$((row * cols + col))
echo ${matrix[$index]} # Outputs: 3
# Define matrix dimensions
rows=2
cols=2
# Iterate over matrix elements
for ((i=0; i<rows; i++)); do
for ((j=0; j<cols; j++)); do
index=$((i * cols + j))
echo -n "${matrix[$index]} "
done
echo
done
# Outputs:
# 1 2
# 3 4
# Define a 3x3 matrix
matrix=(
1 2 3
4 5 6
7 8 9
)
# Matrix dimensions
rows=3
cols=3
# Function to print matrix
print_matrix() {
for ((i=0; i<rows; i++)); do
for ((j=0; j<cols; j++)); do
index=$((i * cols + j))
echo -n "${matrix[$index]} "
done
echo
done
}
# Print the matrix
print_matrix
# Outputs:
# 1 2 3
# 4 5 6
# 7 8 9
# Update an element
matrix[4]=55 # Update element at row 1, column 1
# Print the updated matrix
print_matrix
# Outputs:
# 1 2 3
# 4 55 6
# 7 8 9