matrix column multiplication with rows of another matrix
1 view (last 30 days)
Show older comments
Monika Kok
on 2 May 2016
Commented: Roger Stafford
on 3 May 2016
this is my (9*9) matrix.
1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 0 1 1
1 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1
1 1 1 0 1 1 1 1 1
I would like to multiply entire column 2, 3, and 4 etc. of this matrix with rows of the below column vector matrix one by one.
000
001
010
011
100
101
110
111
This is multiply 000 with column 2 , 3 and 4. which will result entire column 2, 3 and 4 as zero.
Then multiply 001 with column 2 , 3 and 4. which will result entire column 2, 3 as zero and column 4 will remain same. This process should continue for each row.
5 Comments
Image Analyst
on 2 May 2016
The so-called 15*15 matrix is actually 9*9. Are the 000, etc. supposed to be binary numbers, or decimal?
Accepted Answer
Image Analyst
on 2 May 2016
Try this:
m=[...
1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 0 1 1
1 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1
1 1 1 0 1 1 1 1 1]
v = [...
000
001
010
011
100
101
110
111
111]
vm = repmat(v, [1, size(m, 2)])
out = m .* repmat(v, [1, size(m, 2)])
Use bin2dec if your v is actually binary numbers.
0 Comments
More Answers (1)
Roger Stafford
on 2 May 2016
Let M be your 15 x 15 matrix and A be your n x 3 matrix.
B = zeros(size(M,1),3,size(A,1)); % A three-dimensional result
for k = 1:size(A,1)
B(:,:,k) = bsxfun(@times,A(k,:),M(:,2:4));
end
You have asked that a size(M,1) x 3 result be obtained for each row of A, which has forced me to give you a three-dimensional result.
1 Comment
Roger Stafford
on 3 May 2016
Monika, you stated “This is multiply 000 with column 2 , 3 and 4. which will result entire column 2, 3 and 4 as zero.” This means, if I understand you correctly, that you first want the entire nine-element columns 2, 3, and 4 to be made into zeros by multiplication with [0 0 0]. That is a 9 by 3 group of zeros. Then you want the same repeated for each row of your eight rows in your second matrix. I conclude that there should be eight times nine times three results and that is why I produced a three dimensional array as the result. The question is why you have now expressed a preference for a different answer with just nine times three elements.
See Also
Categories
Find more on Matrix Indexing 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!