Octave error: no default value for argument 1 while calling function
Octave error: no default value for argument 1 while calling function
我需要按列为每个值查找 normalized 值。
所以我有这样的功能:
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
for x = 1:size(X, 1)
mu(1,x) = mean(:, x)
sigma(1,x) = std(:, x)
for y = 1:size(X, 2)
X_norm(x,y) = (X(x,y) - mu)/sigma
end
end
end
我正在尝试以这种方式调用此函数:
>> X = [23 32 2 ; 23 23 1];
>> featureNormalize(X)
结果我得到这样的错误:调用函数时参数 1 没有默认值。我不明白这里有什么问题,有什么建议吗?
我认为您使用 mean()
和 std()
函数的方式存在问题,下标赋值也可能是一个问题。
我不确定,但请尝试使用下面这些代码作为您的函数定义:
1.基于行的规范化:
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = zeros(size(X));
mu = zeros(1, size(X, 1));
sigma = zeros(1, size(X, 1));
for x = 1:size(X, 1)
mu(x) = mean( X(x, :) );
sigma(x) = std( X(x, :) );
for y = 1 : size(X, 2)
X_norm(x, y) = ( X(x, y) - mu(x) ) / sigma(x);
end
end
end
2。基于列的归一化:
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = zeros(size(X));
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
for y = 1:size(X, 2)
mu(y) = mean( X(:, y) );
sigma(y) = std( X(:, y) );
for x = 1 : size(X, 1)
X_norm(x, y) = ( X(x, y) - mu(y) ) / sigma(y);
end
end
end
如果您要针对每个 列 的 mean
和 sigma
值计算每个元素的归一化值,我认为您需要由于您使用的是 mean
和 sigma
值的数组,因此您的问题代码并没有真正反映出一些变化。所以,我认为你需要做这样的事情 -
%// Initialize array for final normalized values
X_norm = zeros(size(X));
for x = 1:size(X, 2)
%// Store mean and standard deviation values for each column
mu = mean(X(:, x))
sigma = std(X(:, x))
%// Use an innermost loop to calculate the normalized values
%// for all the elements in each column.
%// Thus, for all these elements, you need to use the same mean
%// and standard deviation calculated in the previous step
for y = 1:size(X, 1)
X_norm(y,x) = (X(y,x) - mu)/sigma
end
end
你可以很容易地向量化上面的代码 bsxfun
-
X_norm = bsxfun(@rdivide, bsxfun(@minus, X, mean(X,1)),std(X,[],1))
我需要按列为每个值查找 normalized 值。 所以我有这样的功能:
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
for x = 1:size(X, 1)
mu(1,x) = mean(:, x)
sigma(1,x) = std(:, x)
for y = 1:size(X, 2)
X_norm(x,y) = (X(x,y) - mu)/sigma
end
end
end
我正在尝试以这种方式调用此函数:
>> X = [23 32 2 ; 23 23 1];
>> featureNormalize(X)
结果我得到这样的错误:调用函数时参数 1 没有默认值。我不明白这里有什么问题,有什么建议吗?
我认为您使用 mean()
和 std()
函数的方式存在问题,下标赋值也可能是一个问题。
我不确定,但请尝试使用下面这些代码作为您的函数定义:
1.基于行的规范化:
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = zeros(size(X));
mu = zeros(1, size(X, 1));
sigma = zeros(1, size(X, 1));
for x = 1:size(X, 1)
mu(x) = mean( X(x, :) );
sigma(x) = std( X(x, :) );
for y = 1 : size(X, 2)
X_norm(x, y) = ( X(x, y) - mu(x) ) / sigma(x);
end
end
end
2。基于列的归一化:
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = zeros(size(X));
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
for y = 1:size(X, 2)
mu(y) = mean( X(:, y) );
sigma(y) = std( X(:, y) );
for x = 1 : size(X, 1)
X_norm(x, y) = ( X(x, y) - mu(y) ) / sigma(y);
end
end
end
如果您要针对每个 列 的 mean
和 sigma
值计算每个元素的归一化值,我认为您需要由于您使用的是 mean
和 sigma
值的数组,因此您的问题代码并没有真正反映出一些变化。所以,我认为你需要做这样的事情 -
%// Initialize array for final normalized values
X_norm = zeros(size(X));
for x = 1:size(X, 2)
%// Store mean and standard deviation values for each column
mu = mean(X(:, x))
sigma = std(X(:, x))
%// Use an innermost loop to calculate the normalized values
%// for all the elements in each column.
%// Thus, for all these elements, you need to use the same mean
%// and standard deviation calculated in the previous step
for y = 1:size(X, 1)
X_norm(y,x) = (X(y,x) - mu)/sigma
end
end
你可以很容易地向量化上面的代码 bsxfun
-
X_norm = bsxfun(@rdivide, bsxfun(@minus, X, mean(X,1)),std(X,[],1))