hi everyone
i have a question
how to divide a (513*513) matrix into smaller 29241 (3*3) matrices ?

 채택된 답변

Adam Danz
Adam Danz 2019년 5월 6일
편집: Adam Danz 2019년 5월 6일

0 개 추천

"how to divide a (513*513) matrix into smaller 29241 (3*3) matrices ?"
"mc" below is a 171 x 171 cell array of your 29241 sub-matrices (each 3x3).
m = magic(513); % fake data 513x513 matrix
mc = mat2cell(m,repmat(3,1,size(m,1)/3), repmat(3,1,size(m,1)/3));
"i want to multiply each submatrix by a matrix of [1 2 4;128 0 8;64 32 16] these weights"
"mcw" below is the same cell array multiplied by your weight matrix.
weight = [1 2 4;128 0 8;64 32 16] ;
mcw = cellfun(@(x)x.*weight, mc, 'UniformOutput', false);

추가 답변 (1개)

Star Strider
Star Strider 2019년 5월 5일

2 개 추천

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

Ahmed Saad
Ahmed Saad 2019년 5월 5일
i tried this method and this is the result but i want separate matrices to perform further calculation on each onematlab screen.png
Star Strider
Star Strider 2019년 5월 5일
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
Ahmed Saad 2019년 5월 6일
i want to multiply each submatrix by a matrix of [1 2 4;128 0 8;64 32 16] these weights
Star Strider
Star Strider 2019년 5월 6일
That is not possible. You are multiplying (9x9) matrices by a (3x3) matrix.
Ahmed Saad
Ahmed Saad 2019년 5월 6일
no i shifted from 9*9 matcries to 3*3, i changed the original question
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.
Adam Danz
Adam Danz 2019년 5월 6일
편집: Adam Danz 2019년 5월 6일
"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
Star Strider 2019년 5월 6일
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
Adam Danz 2019년 5월 6일
I must have first seen the question after it's final form.
+1 for getting the OP there.
Ahmed Saad
Ahmed Saad 2019년 5월 6일
thanks guys for your answers it helped alot
actually i have a 30 different (513*513) matrcies that i need to do the same for each one then add the element in each matrix cell to generate one number from each matrix so i end up with one number for each of the 30 matrcies and then gather them all together in one column and for the multiplication is is an element wise multiplication.
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

질문:

2019년 5월 5일

댓글:

2019년 5월 6일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by