Error using sum.Invalid data type. First argument must be numeric or logical.

12 views (last 30 days)
I want to calculate the mean value but its cannot calculate that and giving error.
%%%%%%%%%%%%%%%%error%%%%%%%%%%%%%
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 116)
y = sum(x, dim, flag) ./ size(x,dim);
Error in featureNormalize (line 7)
mu = mean (X);
Error in trynsl (line 14)
X_n = featureNormalize(X);
%%%%functioncode%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[X_norm,mu,sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
mu = mean (X);
for i=1:size(X, 2)
X(:,i) = X(:,i) - mu(i);
end
sigma = std (X);
for i=1:size(X, 2)
X(:,i) = X(:,i) ./ sigma(i);
end
X_norm = X;
end
  6 Comments
Walter Roberson
Walter Roberson on 5 Mar 2020
Your columns 2, 3, and 4 are character vectors. You cannot normalize character vectors.
Note that your y is a table that contains a single variable that is character vectors.
Note by the way that you can do things like,
fmt_cell = repmat({'%d'}, 1, 43);
fmt_cell([2:4, 42]) = {'%s'};
fmt = [fmt_cell{:}];
data = textscan(fid,fmt,'delimiter',',','CollectOutput',true);
d1 = data{1}; %numeric
d2_4 = data{2}; %character
d5_41 = data{3}; %numeric
y = data{4}; %character
d43 = data{5};
X = [d1, d5_41, d43];
X_n = featureNormalize(X);
Mavra Mehmood
Mavra Mehmood on 5 Mar 2020
it stills not working properly
Error in featureNormalize (line 12)
sigma = std (X);
Error in trynsl (line 25)
X_n = featureNormalize(X);
code
function[X_norm,mu,sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
mu = mean (X);
for i=1:size(X, 2)
X(:,i) = X(:,i) - mu(i);
end
sigma = std(X);
for i=1:size(X, 2)
X(:,i) = X(:,i) ./ sigma(i);
end
X_norm = X;
end

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!