Ex. (210) - Decompose a matrix into multiple matrices based on unique values, is presented here in three programming languages: Python, MATLAB, and JavaScript. Although the implementations differ in syntax, the underlying concept remains identical across all three versions. Each code sample is reproduced from its respective volume of the series Coding Examples from Simple to Complex (Springer, 2024).
This code implementation from above defines a series of variables and functions to manipulate and decompose a matrix c based on an alphabet of unique values found in that matrix. The code also prints the decomposed matrices and the alphabet. The code begins by defining a variable r and initializing it with the value "u." Next, there is a 2D array c, representing a matrix, where each element is a numerical value. This matrix has dimensions 9x10. The code then calls the matrix_alphabet function to extract unique values from the matrix c and stores them in an array b. It also calls the decompose function to decompose the matrix c into a 3D array t. This decomposition is based on the alphabet found in b. A for-loop is used to iterate through the elements of the alphabet array b, and for each unique value in b, it prints a decomposed version of the matrix t using the SMC function. The decompose function takes the matrix c and the alphabet array a as arguments. It creates a new 3D array d and populates it by iterating through the elements of c. For each element in c, it creates a sub-array in d, and for each unique value in a, it assigns the corresponding value from c to the sub-array in d. The matrix_alphabet function extracts unique values from the matrix t and stores them in an array a. It iterates through the elements of t, checks if each value is already in a, and if not, adds it to a. Next, the SMC function is used to create a string representation of a decomposed matrix. It takes a matrix m and an index k representing the alphabet element to consider. It constructs a string r that displays the decomposed matrix with the index k. The decomposed matrix is surrounded by lines and separators for better visualization. The code concludes by printing the alphabet array b and the decomposed matrices for each unique element.
def matrix_alphabet(t):
a = []
n = len(t)
m = len(t[0])
for i in range(n):
for j in range(m):
q = 1
for k in range(len(a) + 1):
if k < len(a) and t[i][j] == a[k]:
q = 0
if q == 1:
a.append(t[i][j])
return a
def decompose(c, a):
n = len(c)
m = len(c[0])
d = []
for i in range(n):
d.append([])
for j in range(m):
d[i].append([])
for k in range(len(a) + 1):
d[i][j].append(" ") # "\u2591"
if k < len(a) and c[i][j] == a[k]:
d[i][j][k] = c[i][j]
return d
def SMC(m, k):
r = 'M' + str(k+1)
r += '\n ----------\n'
for i in range(len(m)):
r += "|"
for j in range(len(m[i])):
r += str(m[i][j][k])
r += "|\n"
r += ' ----------'
return r
# Main code
r = "u"
c = [
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 2, 1, 0, 1, 3, 1, 0, 1, 1],
[1, 1, 2, 0, 1, 3, 0, 1, 0, 1],
[0, 1, 0, 2, 1, 3, 1, 0, 0, 1],
[1, 1, 1, 0, 2, 3, 1, 0, 1, 0],
[1, 0, 1, 1, 1, 3, 0, r, 0, 0],
[1, 0, 3, 3, 3, 3, r, 0, 0, 1],
[1, 0, 1, 1, 1, r, 0, 9, 9, 9],
[1, 1, 0, 0, 0, 0, 1, 9, 0, 9]
]
b = matrix_alphabet(c)
t = decompose(c, b)
for k in range(len(b)):
print(SMC(t, k))
print(b)let r = "u";
let c = [
[ 1, 1, 1, 1, 1, 1, 0, 1, 1, 1 ],
[ 1, 2, 1, 0, 1, 3, 1, 0, 1, 1 ],
[ 1, 1, 2, 0, 1, 3, 0, 1, 0, 1 ],
[ 0, 1, 0, 2, 1, 3, 1, 0, 0, 1 ],
[ 1, 1, 1, 0, 2, 3, 1, 0, 1, 0 ],
[ 1, 0, 1, 1, 1, 3, 0, r, 0, 0 ],
[ 1, 0, 3, 3, 3, 3, r, 0, 0, 1 ],
[ 1, 0, 1, 1, 1, r, 0, 9, 9, 9 ],
[ 1, 1, 0, 0, 0, 0, 1, 9, 0, 9 ]
];
let b = matrix_alphabet(c);
let t = decompose(c, b);
for(let k=0; k<b.length; k++){
print(SMC(t, k));
}
print(b);
function decompose(c, a){
let n = c.length;
let m = c[0].length;
d = [];
for(let i=0; i<n; i++){
d[i] = [];
for(let j=0; j<m; j++){
d[i][j] = [];
for(let k=0; k<=a.length; k++){
d[i][j][k] = " "; // "\u2591"
if (c[i][j] === a[k]) {
d[i][j][k] = c[i][j];
}
}
}
}
return d;
}
function matrix_alphabet(t){
let a = [];
let n = t.length;
let m = t[0].length;
for(let i=0; i<n; i++){
for(let j=0; j<m; j++){
var q = 1;
for(let k=0; k<=a.length; k++){
if (t[i][j] === a[k]) {q = 0;}
}
if (q === 1) {a.push(t[i][j]);}
}
}
return a;
}
function SMC(m, k) {
let r = 'M' + (k+1);
r += '\n ----------\n';
for(let i=0; i<m.length; i++) {
r += "|";
for(let j=0; j<m[i].length; j++) {
r += m[i][j][k];
}
r += "|\n";
}
r += ' ----------';
return r;
}c = {
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1];
[1, 2, 1, 0, 1, 3, 1, 0, 1, 1];
[1, 1, 2, 0, 1, 3, 0, 1, 0, 1];
[0, 1, 0, 2, 1, 3, 1, 0, 0, 1];
[1, 1, 1, 0, 2, 3, 1, 0, 1, 0];
[1, 0, 1, 1, 1, 3, 0, 0, 0, 0];
[1, 0, 3, 3, 3, 3, 0, 0, 0, 1];
[1, 0, 1, 1, 1, 0, 0, 9, 9, 9];
[1, 1, 0, 0, 0, 0, 1, 9, 0, 9]
};
b = matrix_alphabet(c);
t = decompose(c, b);
for k = 1:numel(b)
disp(SMC(t, k));
end
disp(b);
function c = replace_with_nan(c, r)
for i = 1:numel(c)
for j = 1:numel(c{i})
if ischar(c{i}(j)) && c{i}(j) == r
c{i}(j) = NaN;
end
end
end
end
function a = matrix_alphabet(c)
a = [];
for i = 1:numel(c)
for j = 1:numel(c{i})
if all(~ismember(a, c{i}(j)))
a = [a, c{i}(j)];
end
end
end
a(isnan(a)) = [];
end
function d = decompose(c, a)
[n, ~] = size(c);
d = cell(n, numel(a));
for i = 1:n
for k = 1:numel(a)
d{i, k} = '';
for j = 1:numel(c{i})
if isnumeric(c{i}(j)) && ...
c{i}(j) == a(k)
d{i, k} = [d{i, k}, ...
num2str(c{i}(j))];
else
d{i, k} = [d{i, k}, ' '];
end
end
end
end
end
function r = SMC(m, k)
r = sprintf('M%d\n', k);
[n, ~] = size(m);
for i = 1:n
r = sprintf('%s|', r);
for j = 1:numel(m{i, k})
r = sprintf('%s%s', r, m{i, k}(j));
end
r = sprintf('%s|\n', r);
end
endOutput:
M1
----------
|111111 111|
|1 1 1 1 11|
|11 1 1 1|
| 1 1 1 1|
|111 1 1 |
|1 111 |
|1 1|
|1 111 |
|11 1 |
----------
M2
----------
| 0 |
| 0 0 |
| 0 0 0 |
|0 0 00 |
| 0 0 0|
| 0 0 00|
| 0 00 |
| 0 0 |
| 0000 0 |
----------
M3
----------
| |
| 2 |
| 2 |
| 2 |
| 2 |
| |
| |
| |
| |
----------
M4
----------
| |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3333 |
| |
| |
----------
M5
----------
| |
| |
| |
| |
| |
| u |
| u |
| u |
| |
----------
M6
----------
| |
| |
| |
| |
| |
| |
| |
| 999|
| 9 9|
----------
[1, 0, 2, 3, 'u', 9]
- Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in Python, Springer, 2024, pp. 1-245.
- Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in MATLAB, Springer, 2024, pp. 1-255.
- Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in Javascript, Springer, 2024, pp. 1-240.