split big metric into small ones
Show older comments
hi everyone
i have a question
how to divide a (513*513) matrix into smaller 29241 (3*3) matrices ?
Accepted Answer
More Answers (1)
Star Strider
on 5 May 2019
You create a 3D matrix, that is (9,9,3249):
M = rand(513);
R = reshape(M, 9, 9, []);
Rsize = size(R)
Rsize =
9 9 3249
11 Comments
Ahmed Saad
on 5 May 2019
Star Strider
on 5 May 2019
You would have to address them individually.
For example, as ‘R(:,:,1)’, ‘R(:,:,2)’ ... and so for the others.
Depending on what operations you want to do, you might be able to vectorise them. Otherwise, you would need a for loop.
I cannot suggest a method without knowing the calculations you want to do.
Ahmed Saad
on 6 May 2019
Star Strider
on 6 May 2019
That is not possible. You are multiplying (9x9) matrices by a (3x3) matrix.
Ahmed Saad
on 6 May 2019
Star Strider
on 6 May 2019
It would have been straightforward for you to change my code — in my Answer that you originally Accepted — to produce your (3x3) matrices, quite simply:
R = reshape(M, 3, 3, []);
This is trivial.
I then would have used loops to do the multiplications, depending on what you want to do, since there are two ways to do the matrix multiplication: element-wise array multiplication and matrix multiplication, since you did not mention which one you wanted to do:
for k = 1:size(R,3)
Out33(:,:,k) = R(:,:,k).*W; % Out Is (3x3x29241) — Array Multiplication
end
or:
for k = 1:size(R,3)
Out1(:,:,k) = R(:,:,k)*W; % Out Is (3x3x29241) — Matrix Multiplication
end
My code is also more efficient.
The reshape call and each loop require:
Elapsed time is 0.000089 seconds.
Elapsed time is 0.066342 seconds.
Elapsed time is 0.064366 seconds.
respectively.
The mat2cell call and cellfun call require:
Elapsed time is 0.052617 seconds.
Elapsed time is 0.114920 seconds.
respectively.
"I then would have used loops to do the multiplications"
Actually, you wouldn't need a for-loop.
m = magic(513); %fake data
weight = [1 2 4;128 0 8;64 32 16] ;
ma = reshape(m, 3, 3, []);
out = ma .* weight; % assuming element-wise multiplication
This is faster, indeed.
Star Strider
on 6 May 2019
I decided to use loops in the event that the Question changed yet again, to add another requirement or calculation, or to change the matrix size. Again.
Moving target ...
Adam Danz
on 6 May 2019
I must have first seen the question after it's final form.
+1 for getting the OP there.
Ahmed Saad
on 6 May 2019
Star Strider
on 6 May 2019
I am still not certain what you want to do.
If you want to apply the weighting matrix to each sub-matrix and then sum the weighted sub-matrices, it would be more efficient to do the summation first, then multiply by the weighting matrix:
R = reshape(M, 3, 3, []);
RWsum = sum(R,3).*W;
The result is the same.
I do not know how you want to get one number for each (513x513) matrix. If you define it as the sum of the elements of the (3x3) weighted sum of the matrices, convert it to a column vector, then sum the column vector:
MatrixNumber = sum(RWsum(:));
You could then put these into a very efficient for loop to get results for each of your 30 matrices.
Categories
Find more on Numeric Types 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!