How to assign values of each sub matrix from main matriix into another matrix?
3 次查看(过去 30 天)
显示 更早的评论
I have a matrix of size 256*384. If we divide it into 4*4 blocks there will be total of 6144 sub matrices(4*4 size). If we divide it into 2*2 blocks there will be total of 24576 sub matrices(2*2 size). I have to get the first value of each sub matrix( (1,1) value of each sub matrix) into single matrix, second value ( (1,2) value ) of each sub matrix into other single matrix and so on upto last value of each sub matrix( (2,2) value for 2*2 sub matrix and (4,4) value for 4*4 sub matrix ) into single matrix. Example: A=[5 4 1 2;9 5 7 3;1 3 8 5;6 0 4 1]; (4*4 size) Divided into 2*2 submatrices The output should be as follows: B(1)=[5 1;1 8] (All first values of each 2*2 submatrix into one matrix) B(2)=[4 2;3 5] (All second values of each 2*2 submatrix into one matrix) B(3)=[9 7;6 4] (All third values of each 2*2 submatrix into one matrix) B(4)=[5 3;0 1] (All fourth values of each 2*2 submatrix into one matrix) and so on upto last value of each submatrix for large matrix.
0 个评论
采纳的回答
Stephen23
2018-5-31
编辑:Stephen23
2018-5-31
Assuming that the number of input matrix rows/columns are exact multiples of the number of submatrix rows/columns, then this does the job quite nicely:
A = [1,2,1,2;3,4,3,4;1,2,1,2;3,4,3,4] % input array
[Ar,Ac] = size(A);
Nr = 2; % submatrix rows
Nc = 2; % submatrix columns
Sr = Ar/Nr;
Sc = Ac/Nc;
C = cell(Sr,Sc); % preallocate output cell array
for Kr = 1:Sr
for Kc = 1:Sc
C{Kr,Kc} = A(Kr:Sr:end,Kc:Sc:end);
end
end
You can easily concatenate the cell array contents into one numeric array:
Z = cat(3,C{:})
There are probably ways of doing this using reshape and permute, but the loop is easier to understand and is likely reasonably efficient anyway. Tested on the matrix from the original question:
>> A = [1,2,1,2;3,4,3,4;1,2,1,2;3,4,3,4]
A =
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
>> Nr = 2; % submatrix rows
>> Nc = 2; % submatrix columns
...
>> C{:}
ans =
1 1
1 1
ans =
3 3
3 3
ans =
2 2
2 2
ans =
4 4
4 4
And a larger example:
>> A = [1,2,3,4,1,2,3,4;5,6,7,8,5,6,7,8];
>> A = [A;A;A;A]
A =
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
>> Nr = 4; % submatrix rows
>> Nc = 2; % submatrix columns
...
>> C{:}
ans =
1 1
1 1
1 1
1 1
ans =
5 5
5 5
5 5
5 5
ans =
2 2
2 2
2 2
2 2
ans =
6 6
6 6
6 6
6 6
ans =
3 3
3 3
3 3
3 3
ans =
7 7
7 7
7 7
7 7
ans =
4 4
4 4
4 4
4 4
ans =
8 8
8 8
8 8
8 8
3 个评论
Stephen23
2018-5-31
编辑:Stephen23
2018-5-31
"we will have eight 4*2 sub matrices .so we have output upto C(8).(each of size 4*2)"
Nope. If you split the original 8x8 matrix into 4x2 blocks, and put each of the first, second, etc. elements into their own submatrices then you will get a cell array of 2x4 submatrices, not 4x2 submatrices as you state. There is absolutely no reason why the block sizes and the extracted submatrix sizes should be the same (although they might be in some special cases).
Perhaps you got the row and column indices mixed up? Remember: rows * columns !
Consider a simple example:
>> A = [1,2,3;4,5,6];
>> A = [A,A;A,A]
A =
1 2 3 1 2 3
4 5 6 4 5 6
1 2 3 1 2 3
4 5 6 4 5 6
If we split this into blocks of size 2x3, and assign the first element to the first cell, etc, then we will get six submatrices of size 2x2. Note that the extracted submatrices do NOT have the same size as the blocks!
In any case, to get the output that you requested you just need to give the correct submatrix size, which is 2x4 (i.e. exactly what you show in your example output, and following how you defined "submatrix" in your original question):
>> A = [1,2,3,4,1,2,3,4;5,6,7,8,5,6,7,8];
>> A = [A;A;A;A]
A =
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
>> Nr = 2; % submatrix rows
>> Nc = 4; % submatrix columns
...
>> C{:}
ans =
1 3 1 3
1 3 1 3
ans =
5 7 5 7
5 7 5 7
ans =
1 3 1 3
1 3 1 3
ans =
5 7 5 7
5 7 5 7
ans =
2 4 2 4
2 4 2 4
ans =
6 8 6 8
6 8 6 8
ans =
2 4 2 4
2 4 2 4
ans =
6 8 6 8
6 8 6 8
更多回答(1 个)
Ameer Hamza
2018-5-31
Do you want something like this
A=[5 4 1 2;9 5 7 3;1 3 8 5;6 0 4 1];
B{1} = A(1:2:end, 1:2:end);
B{2} = A(1:2:end, 2:2:end);
B{3} = A(2:2:end, 1:2:end);
B{4} = A(2:2:end, 2:2:end);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!