Normalizing columns: Does my function do the same as "normc"?

I don't have access to the Neural Network Toolbox and "normc", so I decided to write my own function which normalizes the columns of a matrix (to length 1). Can somebody please tell me, if my function would deliver the same results as "normc" or would there be a difference for a random matrix "A"?
k = 1:size(A,2)
output(:,k) = A(:,k) ./ norm(A(:,k));
end
When I test my own function on a random matrix I get the following result. Would the real normc deliver the same results?
result = normc_fake([0.3223 0.4342 0.2442; 0.003 0.3323 0.3332; 4.333 2.222 4.444])
result =
0.0742 0.1897 0.0547
0.0007 0.1452 0.0747
0.9972 0.9710 0.9957
Thanks so much.

回答 (2 件)

Star Strider
Star Strider 2017 年 12 月 12 日

1 投票

No, but this one does:
m = [1 2; 3 4];
normc_fcn = @(m) sqrt(m.^2 ./ sum(m.^2));
Q1 = normc(m)
Q2 = normc_fcn(m)
Q1 =
0.3162 0.4472
0.9487 0.8944
Q2 =
0.3162 0.4472
0.9487 0.8944

4 件のコメント

fishandcat
fishandcat 2017 年 12 月 12 日
編集済み: fishandcat 2017 年 12 月 12 日
Thanks so much for your answer.
I just found out that octave-online.net offers the real "normc" function and I tested all versions for the following matrix m. The results of your version differ from my version and the "normc" on octave-online. Also if I use your version in my matlab-code my output-graphic doesn't look right. So are you sure my code isn't right, because actually it delivers the same results as normc on octave-online.net?
m = [-0.3223 0.4342 0.2442; 0.003 -0.3323 0.3332; 4.333 -2.222 -4.444]
My code from above brings the result
-7.4178e-02 1.8975e-01 5.4715e-02
6.9045e-04 -1.4522e-01 7.4656e-02
9.9724e-01 -9.7103e-01 -9.9571e-01
the real normc on octave-online.net
-7.4178e-02 1.8975e-01 5.4715e-02
6.9045e-04 -1.4522e-01 7.4656e-02
9.9724e-01 -9.7103e-01 -9.9571e-01
And your code from above brings the following result: here the minus-signs are not there
7.4178e-02 1.8975e-01 5.4715e-02
6.9045e-04 1.4522e-01 7.4656e-02
9.9724e-01 9.7103e-01 9.9571e-01
Star Strider
Star Strider 2017 年 12 月 12 日
編集済み: Star Strider 2017 年 12 月 12 日
My pleasure.
Mine conforms to the MATLAB version, as I demonstrated. The squaring of the elements and the summation will of course never produce a negative element in the normalised matrix, unless you multiply it element-wise by a sign matrix:
This does what you want:
normc_fcn = @(m) sqrt(m.^2 ./ sum(m.^2)) .* sign(m);
fishandcat
fishandcat 2017 年 12 月 13 日
編集済み: fishandcat 2017 年 12 月 13 日
Thanks so much for taking the time and answering.
I should be able to use my own function from the first post, right? My function brings the exact same results as the real normc (see next post by James Tursa) and your updated normc_fcn with the sign function.
Star Strider
Star Strider 2017 年 12 月 13 日
My pleasure.
You could use your own function.
Because mine is vectorised, it much more efficient and probably faster. You can easily create a function file out of it:
function nm = normc_fcn(m)
nm = sqrt(m.^2 ./ sum(m.^2)) .* sign(m);
end

サインインしてコメントする。

James Tursa
James Tursa 2017 年 12 月 12 日

0 投票

Yes, your algorithm matches normc (R2016b Win64):
>> m
m =
-0.3223 0.4342 0.2442
0.0030 -0.3323 0.3332
4.3330 -2.2220 -4.4440
>> normc(m)
ans =
-0.0742 0.1897 0.0547
0.0007 -0.1452 0.0747
0.9972 -0.9710 -0.9957
>> for k=1:size(m,2)
output(:,k) = m(:,k)/norm(m(:,k));
end
>> output
output =
-0.0742 0.1897 0.0547
0.0007 -0.1452 0.0747
0.9972 -0.9710 -0.9957
Star's algorithm produces something different:
>> normc_fcn = @(m) sqrt(m.^2 ./ sum(m.^2));
>> normc_fcn(m)
ans =
0.0742 0.1897 0.0547
0.0007 0.1452 0.0747
0.9972 0.9710 0.9957

2 件のコメント

fishandcat
fishandcat 2017 年 12 月 13 日
Thank you very much. So I can use my own function from the first post.
James Tursa
James Tursa 2017 年 12 月 13 日
編集済み: James Tursa 2017 年 12 月 13 日
You could also use this for later versions of MATLAB:
my_normc = @(m)m./sqrt(sum(m.^2))
or this for earlier versions of MATLAB:
my_normc = @(m)bsxfun(@rdivide,m,sqrt(sum(m.^2)))
Again, this comes with the caveat that each column has at least one non-zero. None of the algorithms posted will match normc for a column that has all 0's. E.g.,
>> m = randi(5,5,5)
m =
4 4 5 3 3
4 1 4 2 3
2 2 2 4 4
4 1 5 4 4
1 1 1 1 4
>> m(:,1) = 0
m =
0 4 5 3 3
0 1 4 2 3
0 2 2 4 4
0 1 5 4 4
0 1 1 1 4
>> my_normc = @(m)bsxfun(@rdivide,m,sqrt(sum(m.^2)));
>> normc(m)
ans =
1.0000 0.8341 0.5934 0.4423 0.3693
1.0000 0.2085 0.4747 0.2949 0.3693
1.0000 0.4170 0.2374 0.5898 0.4924
1.0000 0.2085 0.5934 0.5898 0.4924
1.0000 0.2085 0.1187 0.1474 0.4924
>> my_normc(m)
ans =
NaN 0.8341 0.5934 0.4423 0.3693
NaN 0.2085 0.4747 0.2949 0.3693
NaN 0.4170 0.2374 0.5898 0.4924
NaN 0.2085 0.5934 0.5898 0.4924
NaN 0.2085 0.1187 0.1474 0.4924

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

タグ

質問済み:

2017 年 12 月 12 日

編集済み:

2017 年 12 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by