Matrix dimension reduction based on summation
8 次查看(过去 30 天)
显示 更早的评论
Hi,
I have 81*81 matrix. I need to reduce this matrix dimension to, let say, 10*10. Let suppose in the new 10*10 matrix, 1st column is equal to the sum of 1 to 10 columns, similarly 2nd column (in the new matrix) is the sum of 11 to 20 columns and so on. Similarly for the rows too.
How we can code this in MATLAB.
Thanks
2 个评论
pfb
2015-4-14
a column in a 10x10 matrix has 10 entries. A column in a 81x81 matrix has 81 entries. How can a the sum of 10 81x1 vectors be a 10x1 vector?
采纳的回答
David Young
2015-4-14
If you try to reduce an 81 x 81 matrix to a 10 x 10 matrix it is bound to be messy, because 81 is not a multiple of 10. You have one row and one column left over.
Instead, let's suppose you want to reduce an 80 x 80 matrix to 10 x 10 by summing the values in each 8 x 8 block. There are various ways of doing this - here's one that uses accumarray:
% Test data
a = rand(80, 80);
% The factors by which to reduce the size
rowReduce = 8; % first dimension
colReduce = 8; % second dimension
% Get the subscripts for each element of the input matrix
[subsAi, subsAj] = ndgrid(1:size(a,1), 1:size(a,2));
% Compute the corresponding output subscripts
subsBi = 1 + floor((subsAi-1)/rowReduce);
subsBj = 1 + floor((subsAj-1)/colReduce);
% Carry out the summation
b = accumarray([subsBi(:) subsBj(:)], a(:)); % b is reduced matrix
% See if the overall summation was correct
disp(abs(sum(a(:)) - sum(b(:)))); % should be small
The code above will work with your original problem, which is to reduce an 81 x 81 matrix. You can still set the reduction factor to get a 10 x 10 result, but the blocks that are summed to produce each output value will vary in size.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!