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
2017-12-12
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
2017-12-12
编辑:fishandcat
2017-12-12
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
2017-12-13
编辑:fishandcat
2017-12-13
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
2017-12-12
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
2017-12-13
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 Exchange 中查找有关 Deep Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!