Matrix dimension reduction based on summation
9 views (last 30 days)
Show older comments
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 Comments
pfb
on 14 Apr 2015
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?
Accepted Answer
David Young
on 14 Apr 2015
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 Comments
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!