Multiply each column in MxN matrix with it's transpose to create MxMxN matrix

6 ビュー (過去 30 日間)
T
T 2017 年 7 月 28 日
編集済み: Andrei Bobrov 2017 年 7 月 28 日
Hello,
I have a MxN matrix, and I need to multiply each column vector that is of size Mx1 with it's transpose. This gives me a MxM matrix for each column. I can do this in a for loop, to create this MxM matrix for each column and to save it in a MxMxN matrix. But I would like to speed this up because the for loop is taking too long. Any ideas or hints?
Thanks!

回答 (2 件)

James Tursa
James Tursa 2017 年 7 月 28 日
編集済み: James Tursa 2017 年 7 月 28 日
X = your MxN matrix
[M,N] = size(X);
Xcols = reshape(X,M,1,N);
Xrows = reshape(X,1,M,N);
result = bsxfun(@times,Xcols,Xrows);
Or if you have R2016b or later, that last line can be
result = Xcols .* Xrows;

Andrei Bobrov
Andrei Bobrov 2017 年 7 月 28 日
編集済み: Andrei Bobrov 2017 年 7 月 28 日
Let A - your matrix [M x N]
out = bsxfun(@times,permute(A,[1,3,2]),permute(A,[3,1,2]));

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by