How to decompose a 9x9 matrix to 3x3 matrices? - matlab

I'm trying to decompose a 9x9 matrix into 9 3x3 matrices. I've already used the command reshape, but the matrices it returns are the ones as transforming the columns of the 9x9 matrix to 3x3 matrices, but that isn't what I need. This is 9x9 the matrix M
0 0 0 0 0 0 0 7 6
0 2 0 9 0 0 0 0 0
0 3 8 0 5 4 1 0 0
9 0 0 5 0 0 0 3 0
0 0 0 0 1 8 0 6 7
4 0 0 0 0 0 2 0 0
0 1 0 8 0 0 0 5 0
0 0 0 0 0 7 0 0 0
6 0 2 0 3 0 8 9 4
And I need it in form
0 0 0 0 0 0 0 7 6
0 2 0 9 0 0 0 0 0
0 3 8 0 5 4 1 0 0 etc...
This code generates the matrices exactly as I need them, but only saving the last one:
for i=[1 4 7]
for j=[1 4 7]
v(:,:)=M([i:i+2],[j:j+2])
end
end

You make a new v of size 3x3 every loop iteration, i.e. you overwrite it. What you can easily do is to add a third dimension, and store all your separate matrices along there:
n = 3; % Amount of rows of the sub-matrices
m = 3; % Amount of columns of the sub-matrices
v = zeros(size(M,1)./n,size(M,2)./m,(size(M,1)*size(M,2))./(n*m)); % Initialise v
kk = 1; % Initialise page counter
for ii = 1:n:size(M,1)
for jj = 1:m:size(M,2)
v(:,:,kk) = M(ii:ii+2, jj:jj+2); % Save on page kk
kk = kk+1; % Increase page counter
end
end
A few other notes:
In order to generalise the code I used size(M) everywhere, such that you can easily extend M to larger sizes.
Don't use i or j as indices/variables names, as they denote the imaginary unit. Using those can easily lead to hard to debug errors.
ii:ii+2 already is an array, adding square brackets is superfluous
Initialise v, i.e. tell MATLAB how large it will be before going into the loop. Otherwise, MATLAB will have to increase its size every iteration,which is very slow. This is called preallocation.
n and m obviously need to be integers, but not only that. You need an integer amount of sub-matrices to fill each dimension, i.e. size(M,1)/n and size(M,2)/m both need to be integers. If this is not the case, this code will error out on the initialisation of v.

You can also use mat2cell function. You need to declare the input_size and the chunk_size. Then you need to declare the number of chunks in each dimension (sc variable).
For example:
M = [0 0 0 0 0 0 0 7 6
0 2 0 9 0 0 0 0 0
0 3 8 0 5 4 1 0 0
9 0 0 5 0 0 0 3 0
0 0 0 0 1 8 0 6 7
4 0 0 0 0 0 2 0 0
0 1 0 8 0 0 0 5 0
0 0 0 0 0 7 0 0 0
6 0 2 0 3 0 8 9 4];
% Z = zeros(size(M, 1), size(M, 2));
input_size = size(M);
chunk_size = [3 3];
sc = input_size ./ chunk_size;
C = mat2cell(M, chunk_size(1)*ones(sc(1), 1),...
chunk_size(2)*ones(sc(2), 1));
celldisp(C')
Output:
ans{1,1} =
0 0 0
0 2 0
0 3 8
ans{2,1} =
0 0 0
9 0 0
0 5 4
ans{3,1} =
0 7 6
0 0 0
1 0 0
ans{1,2} =
9 0 0
0 0 0
4 0 0
ans{2,2} =
5 0 0
0 1 8
0 0 0
ans{3,2} =
0 3 0
0 6 7
2 0 0
ans{1,3} =
0 1 0
0 0 0
6 0 2
ans{2,3} =
8 0 0
0 0 7
0 3 0
ans{3,3} =
0 5 0
0 0 0
8 9 4

You can create a tiled index matrix, then use this to get the 3*3 arrays
idx = repelem( reshape(1:9,3,3), 3, 3 );
out = arrayfun( #(x) reshape(A(idx==x),3,3), 1:9, 'uni', 0 );
Explanation:
idx is a tiled array as shown:
idx =
1 1 1 4 4 4 7 7 7
1 1 1 4 4 4 7 7 7
1 1 1 4 4 4 7 7 7
2 2 2 5 5 5 8 8 8
2 2 2 5 5 5 8 8 8
2 2 2 5 5 5 8 8 8
3 3 3 6 6 6 9 9 9
3 3 3 6 6 6 9 9 9
3 3 3 6 6 6 9 9 9
Then the arrayfun line loops through the values 1 .. 9, and extracts the matrix where idx matches the indexing value of the loop. We have to use a final reshape here because the logical index turns the array 1D.
The output is a 9 by 1 cell array, where each cell is a submatrix.

Using only linear indexing and implicit expansion:
% Random example matrix
M = randi(9,[9,12])
% Block size
n = 3;
% Indexing
s = size(M,1)
ind = [1:n].'+[0:n-1]*s+reshape(floor((0:n:(numel(M)/n)-1)/s)*n*s+mod(1:n:numel(M)/n,s)-1,1,1,[])
% Split each block into a third dimension.
B = M(ind)
Where:
[1:n].'+[0:n-1]*s =
1 10 19
2 11 20
3 12 21
% The linear index of the first block
And:
reshape(floor((0:n:(numel(M)/n)-1)/s)*n*s+mod(1:n:numel(M)/n,s)-1,1,1,[]) =
ans(:,:,1) = 0
ans(:,:,2) = 3
ans(:,:,3) = 6
ans(:,:,4) = 27
ans(:,:,5) = 30
ans(:,:,6) = 33
ans(:,:,7) = 54
...
% The shifting number for each block
Noticed that the length of each dimension of the matrix M has to be divisible by n

#java
// method decompose a 9*9 matrix to 3*3 matrices
public static int[][][] decompose(int[][] matrix) {
int[][][] result = new int[9][3][3];
for (int i = 0; i < 9; ) {
for (int j = 0; j < 9; j += 3) {
for (int k = 0; k < 9; k += 3) {
for (int l = j; l < j + 3; l++) {
for (int m = k; m < k + 3; m++) {
result[i][l % 3][m % 3] = (matrix[l][m]);
}
}
i++;
}
}
}
return result;
}
For example:
int[][] matrix = {
{1, 2, 3, 10, 11, 12, 19, 20, 21},
{4, 5, 6, 13, 14, 15, 22, 23, 24},
{7, 8, 9, 16, 17, 18, 25, 26, 27},
{28, 29, 30, 37, 38, 39, 46, 47, 48},
{31, 32, 33, 40, 41, 42, 49, 50, 51},
{34, 35, 36, 43, 44, 45, 52, 53, 54},
{55, 56, 57, 64, 65, 66, 73, 74, 75},
{58, 59, 60, 67, 68, 69, 76, 77, 78},
{61, 62, 63, 70, 71, 72, 79, 80, 81}
};
int[][][] test = decompose(matrix);
for (int[][] grid : test) {
for (int[] gridLine : grid) {
for (int i = 0; i < 3; i++) {
System.out.print(gridLine[i] + " ");
}
System.out.println();
}
System.out.println("========");
}
Output:
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
========

Related

How can I make a diamond of zeroes in a matrix of any size? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a square Matrix N x M, odd dimensions, and I want to put a diamond of zeroes, for example, for a 5 x 5 matrix:
1 3 2 4 2
5 7 8 9 5
3 2 4 6 3
6 8 2 1 3
3 3 3 3 3
Is transform to:
1 3 0 4 2
5 0 8 0 5
0 2 4 6 0
6 0 2 0 3
3 3 0 3 3
How can this be done efficiently?
I'll bite, here is one approach:
% NxN matrix
N = 5;
assert(N>1 && mod(N,2)==1);
A = magic(N);
% diamond mask
N2 = fix(N/2);
[I,J] = meshgrid(-N2:N2);
mask = (abs(I) + abs(J)) == N2;
% fill with zeros
A(mask) = 0;
The result:
>> A
A =
17 24 0 8 15
23 0 7 0 16
0 6 13 20 0
10 0 19 0 3
11 18 0 2 9
I also had some time to play around. For my solution there are no limits concerning A being odd or even or larger than 1. Every integer is fine (even 0 works, though it does not make sense).
% NxN matrix
N = 7;
A = magic(N);
half = ceil( N/2 );
mask = ones( half );
mask( 1 : half+1 : half*half ) = 0;
mask = [ fliplr( mask ) mask ];
mask = [ mask; flipud( mask ) ];
if( mod(N,2) == 1 )
mask(half, :) = []
mask(:, half) = []
end
A( ~mask ) = 0;
A
I am first creating a square sub-matrix mask of "quarter" size (half the number of columns and half the number of rows, ceil() to get one more in the case N is odd).
Example for N=7 -> half=4.
mask =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
I then set it's diagonal values to zero:
mask =
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
Mirror the mask horizontally:
mask =
1 1 1 0 0 1 1 1
1 1 0 1 1 0 1 1
1 0 1 1 1 1 0 1
0 1 1 1 1 1 1 0
Then mirror it vertically:
mask =
1 1 1 0 0 1 1 1
1 1 0 1 1 0 1 1
1 0 1 1 1 1 0 1
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
1 0 1 1 1 1 0 1
1 1 0 1 1 0 1 1
1 1 1 0 0 1 1 1
As N is odd we got a redundant row and redundant column that are then removed:
mask =
1 1 1 0 1 1 1
1 1 0 1 0 1 1
1 0 1 1 1 0 1
0 1 1 1 1 1 0
1 0 1 1 1 0 1
1 1 0 1 0 1 1
1 1 1 0 1 1 1
The logical not is then used as a mask to select the values in the original matrix that are set to 0.
Probably not as efficient as #Amro's solution, but it works. :D
My solution:
looking at the first left half of the matrix
in the first row 0 is in the middle column (let's call it mc)
in the second row the 0is in column mc-1
and so on while the rows increase
when you reach column 1 the sequence continue but with mc+1 but the rows decrease
In a similar way for the right half of the matrix
n=7
a=randi([20 30],n,n)
% Centre of the matrix
p=ceil(n/2)
% Identify the column sequence
col=[p:-1:1 2:p p+1:n n-1:-1:p]
% Identify the row sequence
row=[1:n n-1:-1:1]
% Transorm the row and column index in linear index
idx=sub2ind(size(a),row,col)
% Set the 0'
a(idx)=0
a =
22 29 23 27 27 21 23
29 29 21 27 24 26 24
30 28 21 27 29 28 25
28 22 24 20 27 24 25
23 26 21 20 30 20 29
26 20 26 23 25 22 25
21 24 25 25 23 21 30
a =
22 29 23 0 27 21 23
29 29 0 27 0 26 24
30 0 21 27 29 0 25
0 22 24 20 27 24 0
23 0 21 20 30 0 29
26 20 0 23 0 22 25
21 24 25 0 23 21 30
Hope this helps.
Qapla'
Using indexing (only works when N is odd):
N = 7;
% Random matrix
A = randi(100, N);
idx = [N-1:-2:1; 2:2:N];
A(cumsum([ceil(N/2) idx(:)' idx(end-1:-1:1)])) = 0
A =
60 77 74 0 54 83 9
8 48 0 76 0 28 67
6 0 32 78 83 0 10
0 27 25 5 11 39 0
76 0 49 43 67 0 16
79 7 0 86 0 70 78
57 28 85 0 81 44 81

Taking a matrix and retrieving both the diagonals keeping the dimensions of the original matrix in MATLAB

Given the matrix A = magic(5) you get:
A = 17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
I want to use commands such as rot90, diag, triu, tril and matrices sum to get the matrix:
A = 17 0 0 0 15
0 5 0 14 0
0 0 13 0 0
0 12 0 21 0
11 0 0 0 9
Please, if you can't think of a way to solve this without the commands I wrote, it's OK to do it your own way.
You can use eye function for indexing
>> A(~eye(size(A)) & ~flipud(eye(size(A))))=0
A =
17 0 0 0 15
0 5 0 14 0
0 0 13 0 0
0 12 0 21 0
11 0 0 0 9
You can simply use linear indexing to access the diagonals:
n = size(A,1);
B = zeros(n);
B( 1:(n+1):end ) = A( 1:(n+1):end ); %// main diagonal
B( n:(n-1):(end-n+1) ) = A( n:(n-1):(end-n+1) )
And you get
B =
17 0 0 0 15
0 5 0 14 0
0 0 13 0 0
0 12 0 21 0
11 0 0 0 9
Another approach is:
mDiag = diag(diag(A));
aDiag = rot90(diag(diag(rot90(A))))';
overlap = A.*((diag(diag(A)) ~= 0) & (rot90(diag(diag(rot90(A)))) ~= 0));
solution = mDiag + aDiag - overlap
And than:
solution =
17 0 0 0 15
0 5 0 14 0
0 0 13 0 0
0 12 0 21 0
11 0 0 0 9
Using bsxfun
out = A.*bsxfun(#(x,y) x == y | x+y == size(A,1)+1,(1:size(A,1)).',1:size(A,1)) %//'

Is it possible to rotate a matrix by 45 degrees in matlab

i.e. so that it appears like a diamond. (it's a square matrix) with each row having 1 more element than the row before up until the middle row which has the number of elements equal to the dimensions of the original matrix, and then back down again with each row back to 1?
A rotation is of course not possible as the "grid" a matrix is based on is regular.
But I remember what your initially idea was, so the following will help you:
%example data
A = magic(5);
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
d = length(A)-1;
diamond = zeros(2*d+1);
for jj = d:-2:-d
ii = (d-jj)/2+1;
kk = (d-abs(jj))/2;
D{ii} = { [zeros( 1,kk ) A(ii,:) zeros( 1,kk ) ] };
diamond = diamond + diag(D{ii}{1},jj);
end
will return the diamond:
diamond =
0 0 0 0 17 0 0 0 0
0 0 0 23 0 24 0 0 0
0 0 4 0 5 0 1 0 0
0 10 0 6 0 7 0 8 0
11 0 12 0 13 0 14 0 15
0 18 0 19 0 20 0 16 0
0 0 25 0 21 0 22 0 0
0 0 0 2 0 3 0 0 0
0 0 0 0 9 0 0 0 0
Now you can again search for words or patterns row by row or column by column, just remove the zeros then:
Imagine you extract a single row:
row = diamond(5,:)
you can extract the non-zero elements with find:
rowNoZeros = row( find(row) )
rowNoZeros =
11 12 13 14 15
Not a real diamond, but probably useful as well:
(Idea in the comments by #beaker. I will remove this part, if he is posting it by himself.)
B = spdiags(A)
B =
11 10 4 23 17 0 0 0 0
0 18 12 6 5 24 0 0 0
0 0 25 19 13 7 1 0 0
0 0 0 2 21 20 14 8 0
0 0 0 0 9 3 22 16 15

Reshape / Transform an upper triangular matrix in MATLAB

I have an upper triangular matrix (without the diagonal) given by:
M = [0 3 2 2 0 0; 0 0 8 6 3 2; 0 0 0 3 2 1; 0 0 0 0 2 1; 0 0 0 0 0 0]
The resulting matrix should look like this:
R = [0 0 0 0 0 0; 0 2 0 0 0 0; 2 3 1 0 0 0; 2 6 2 1 0 0; 3 8 3 2 0 0]
Since I couldn't find a simple explanation which describes my goal I tried to visualize it with an image:
I already tried lots of different combinations of rot90, transpose, flipud etc., but I could't find the right transformation that gives me the matrix R
EDIT:
The rows of the matrix M are not always sorted as in the example above. For another matrix M_2:
M_2 = [0 2 3 1 0 0; 0 0 3 6 3 9; 0 0 0 1 2 4; 0 0 0 0 2 6; 0 0 0 0 0 0]
the resulting matrix R_2 need to be the following:
R_2 = [0 0 0 0 0 0; 0 9 0 0 0 0; 1 3 4 0 0 0; 3 6 2 6 0 0; 2 3 1 2 0 0]
Again the visualization below:
EDIT:
Inspired by the tip from #Dan's comment, it can be further simplified to
R = reshape(rot90(M), size(M));
Original Answer:
This should be a simple way to do this
F = rot90(M);
R = F(reshape(1:numel(M), size(M)))
which returns
R =
0 0 0 0 0 0
0 2 0 0 0 0
2 3 1 0 0 0
2 6 2 1 0 0
3 8 3 2 0 0
The idea is that when you rotate the matrix you get
>> F = rot90(M)
F =
0 2 1 1 0
0 3 2 2 0
2 6 3 0 0
2 8 0 0 0
3 0 0 0 0
0 0 0 0 0
which is a 6 by 5 matrix. If you consider the linear indexing over F the corresponding indices are
>> reshape(1:30, size(F))
1 7 13 19 25
2 8 14 20 26
3 9 15 21 27
4 10 16 22 28
5 11 17 23 29
6 12 18 24 30
where elements 6, 11, 12, 16, 17, 18 , and ... are zero now if you reshape this to a 5 by 6 matrix you get
>> reshape(1:30, size(M))
1 6 11 16 21 26
2 7 12 17 22 27
3 8 13 18 23 28
4 9 14 19 24 29
5 10 15 20 25 30
Now those elements corresponding to zero values are on top, exactly what we wanted. So by passing this indexing array to F we get the desired R.
Without relying on order (just rotating the colored strips and pushing them to the bottom).
First solution: note that it doesn't work if there are zeros between the "data" values (for example, if M(1,3) is 0 in the example given). If there may be zeros please see second solution below:
[nRows nCols]= size(M);
R = [flipud(M(:,2:nCols).') zeros(nRows,1)];
[~, rowSubIndex] = sort(~~R);
index = sub2ind([nRows nCols],rowSubIndex,repmat(1:nCols,nRows,1));
R = R(index);
Second solution: works even if there are zeros within the data:
[nRows nCols]= size(M);
S = [flipud(M(:,2:nCols).') zeros(nRows,1)];
mask = 1 + fliplr(tril(NaN*ones(nRows, nCols)));
S = S .* mask;
[~, rowSubIndex] = sort(~isnan(S));
index = sub2ind([nRows nCols],rowSubIndex,repmat(1:nCols,nRows,1));
R = S(index);
R(isnan(R)) = 0;
Alternate option, using loops:
[nRows nCols]= size(M);
R = zeros(nRows,nCols);
for n = 1:nRows
R((n+1):nCols,n)=fliplr(M(n,(n+1):nCols));
end

How to get the unique slices of a matrix?

In matlab, if you have a matrix A you can find the matrix B containing all of the unique rows of A as follows:
B = unique(A,'rows');
What I have is a 3d matrix, with rows and columns as the first two dimensions, and one additional dimension ('slices').
How can I get the 3d matrix containing all the unique slices in a matrix A? Here's an example of the kind of functionality I want:
>> A % print out A
A(:,:,1) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,3) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,4) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
>> unique(A,'slices'); % get unique slices
A(:,:,1) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,3) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
I would begin by reshaping A so each slice becomes a row (with the reshape command). Then use unique(A, 'rows'). Finally, reshape the unique rows back to the same shape the slices.
For example:
% transforming so each row is a slice in row form
reshaped_A = reshape(A, [], size(A, 3))';
% getting unique rows
unique_rows = unique(reshaped_A, 'rows');
% reshaping back
unique_slices = reshape(unique_rows', size(A, 1), size(A, 2), []);
Or all in one line:
reshape(unique(reshape(A, [], size(A, 3))', 'rows')', size(A, 1), size(A, 2), [])
I haven't checked this above code so use with caution! But it should give the idea.
EDIT
Here it is working on your data (also fixed little bug in above code):
>> reshaped_A = reshape(A, [], size(A, 3))'
reshaped_A =
Columns 1 through 11
16 5 9 4 2 11 7 14 3 10 6
1 0 0 0 0 1 0 0 0 0 1
16 5 9 4 2 11 7 14 3 10 6
0 0 0 1 0 0 1 0 0 1 0
Columns 12 through 16
15 13 8 12 1
0 0 0 0 1
15 13 8 12 1
0 1 0 0 0
Each of these ^^ rows is one of the original slices
>> unique_rows = unique(reshaped_A, 'rows')
unique_rows =
Columns 1 through 11
0 0 0 1 0 0 1 0 0 1 0
1 0 0 0 0 1 0 0 0 0 1
16 5 9 4 2 11 7 14 3 10 6
Columns 12 through 16
0 1 0 0 0
0 0 0 0 1
15 13 8 12 1
These ^^ are the unique slices, but in the wrong shape.
>> unique_slices = reshape(unique_rows', size(A, 1), size(A, 2), [])
unique_slices(:,:,1) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
unique_slices(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
unique_slices(:,:,3) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A very simple and scalable solution would be:
A = cat(3, [16 2 3 13;5 11 10 8;9 7 6 12;4 14 15 1], [1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1], [16 2 3 13;5 11 10 8;9 7 6 12;4 14 15 1], [0 0 0 1;0 0 1 0;0 1 0 0;1 0 0 0])
[n,m,p] = size(A);
a = reshape(A,n,[],1);
b = reshape(a(:),n*m,[])';
c = unique(b,'rows', 'stable')'; %If the 'stable' option is supported by your version.
%If the 'stable' option is not supported, but it's still required, use the index vector option, as required.
%i.e.,
%[c,I,J] = unique(b,'rows');
unique_A = reshape(c,n,m,[])
Results:
A(:,:,1) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,3) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,4) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
unique_A(:,:,1) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
unique_A(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
unique_A(:,:,3) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Source: How to find unique pages in a 3d matrix?