Understanding Matrix Notation/Arithmetic

6 ビュー (過去 30 日間)
James Pistorino
James Pistorino 2017 年 2 月 22 日
編集済み: Jan 2017 年 2 月 22 日
I am a Matlab newbie, so please be gentle.
I am attempting to port some Matlab code to C++ and am not following some of the syntax.
Here is the code I am porting:
[ndata, dimx] = size(x);
[ncentres, dimc] = size(c);
if dimx ~= dimc
error('Data dimension does not match dimension of centres')
end
n2 = (ones(ncentres, 1) * sum((x.^2)', 1))' + ...
ones(ndata, 1) * sum((c.^2)',1) - ...
2.*(x*(c'));
In this code, x and c are both two dimensional matrices, each with two columns and many rows.
I am getting lost on the n2 line.
Focusing on the clause: "sum((x.^2)',1))'".
I believe that squares every element in the x matrix.
Then transpose the matrix (resulting in a 2xN matrix).
Then sum each column of the matrix (resulting in a 1xN matrix).
Then transpose again (resulting in a Nx1 matrix).
Is this correct?
Thanks for any help.

採用された回答

Jan
Jan 2017 年 2 月 22 日
編集済み: Jan 2017 年 2 月 22 日
Almost. You need the first part also:
(ones(ncentres, 1) * sum((x.^2)', 1))'
sum((x.^2)', 1) this is: square elements, transpose, sum over 1st dimension.
Then it is mutliplied with a [M x 1] matrix consisting of 1s from the right and transposed afterwards. Shorter (and faster):
sum(x.^2, 2) * ones(1, ncentres)
The multiplication repeates the columns only, so this is equivalent to:
repmat(sum(x.^2, 2), 1, ncentres)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by