Multiply parts of matrices

Hello everyone - many thanks for taking the time to read this.
Matrix A is 63x63 and Matrix B is 63x7
I want to multiply every 9x9 of Matrix A with every 9x1 of Matrix B.
So the first:
9x9 in matrix A with the first 9x1 in Matrix B
1 2 3 4 5 6 7 8 9 1
1 2 3 4 5 6 7 8 9 2
1 2 3 4 5 6 7 8 9 3
1 2 3 4 5 6 7 8 9 4
1 2 3 4 5 6 7 8 9 5
1 2 3 4 5 6 7 8 9 6
1 2 3 4 5 6 7 8 9 7
1 2 3 4 5 6 7 8 9 8
1 2 3 4 5 6 7 8 9 9
Then the second 9x9 to the right in matrix A with the second 9x1 in matrix B.
I'm currently doing it using cells which is great but need to be able to do it outside of cell format.
What would you recommend?

2 Comments

What's wrong with using cells?
Absolutely nothing but this has to go in part of a loop where using cells has proved to be very difficult.

Sign in to comment.

 Accepted Answer

variant 1
A = randi(100,8,6);
B = randi(50,4,3);
[m n] = size(A);
q = 4;
p = 2;
a1 = reshape(permute(reshape(A,q,m/q,p,[]),[1 3 2 4]),q,p,[]);
out = reshape(sum(bsxfun(@times,a1,reshape(B,1,p,[])),2),m,[]);

More Answers (1)

Walter Roberson
Walter Roberson on 30 Nov 2011

2 votes

When you say multiply the 9x9 by the 9x1, is that a matrix multiplication (which would output a 9x1), or is that an element-by-element multiplication producing 9 x 9 with each row in A multiplied the (single) value from the corresponding column in B ?

4 Comments

I believe it is matrix multiplication. The total output will be 63x7..which is the 7 (9x1)s next to eachother. Each column of matrix B will only be multiplied with its corresponding 9x9. A(1:9,1:9)*B(1:9,1)...A(1:9,10:18)*B(1:9,2)....I hope I'm making sense. Should I dismantle the matricess into 9x9s and 9x1s or can this be done simply in their large matrix forms.
If you have a MATLAB from about 2009b onwards, you can use http://www.mathworks.com/help/toolbox/images/ref/blockproc.html blockproc on A. Let it break up A in to 9 x 9 arrays for you, handing you the A block data as block_struct.data . Look at the row and column indices handed to you as block_struct.location. Calculate Bcol = 1+(block_struct.location(2)/9) and then the appropriate subsection of B will be B(block_struct.location(1):block_struct.location(1)+8, Bcol) . Do the appropriate matrix multiplication and let that be the output of your routine.
Brill, I'll check that out. Thanks!
Correction:
Bcol = 1 + (block_struct.location(2) - 1)/9

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!