matrix multiplication

Assume we have the following matrix A = [ 1 2; 3 4; 5 6]; and we want to do the following multiplication
[ A(:,1)*A(:,1)';A(:,2)*A(:,2)'] without loop. So the result should be like this B =
1 3 5
3 9 15
5 15 25
4 8 12
8 16 24
12 24 36
Could you please help me with this problem.
Thanks

댓글 수: 1

James Tursa
James Tursa 2012년 1월 28일
Ummm ... you already did. I presume you really have a more general question in mind? Like a larger size variable? Or ...?

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

 채택된 답변

James Tursa
James Tursa 2012년 1월 28일

0 개 추천

Another way:
[m n] = size(A);
Ar = reshape(A,m,1,n);
B = reshape(mtimesx(Ar,Ar,'t'),m,m*n).'
You can find mtimesx on the FEX here:
href=""<http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support</a>>
Is there a reason why you need the result stacked vertically as you have it? The last transpose is moving all of the data around in memory. You could avoid it if you stacked the results horizontally instead.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2012년 1월 28일

0 개 추천

B = reshape(bsxfun(@times,permute(A,[1 3 2]),permute(A,[3 1 2])),size(A,1),[]).'

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2012년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by