Norm and mean of a time series

5 次查看(过去 30 天)
I am very new to matlab and have a time series x1 and this time series should have zero mean and unit norm. I have been provided with the formula given below to calculate norm and mean But when i run the program it gives me the error shown below. Please help me to resolve the problem.
clear all; close all; clc;
x1=[11500.2 11477.9 11417.3 11426.4 11413 11382.9 11375.1 11347.9 11351.1 11329.3]
mean=mean(x1)
for i=1:length(x1)
x1(i)=(x1(i)-mean)/norm
end
norm=norm(x1)
for i=1:length(x1)
x1(i)=x1(i)/norm
end
%%%Error%%%%
Error using norm
Not enough input arguments.
Error in DMN_ROI (line 27)
x1(i)=(x1(i)-mean)/norm
  1 个评论
Atsushi Ueno
Atsushi Ueno 2021-5-18
编辑:Atsushi Ueno 2021-5-18
The error occurs because you are using the variable "norm" before defining it.
x1(i)=(x1(i)-mean)/norm
In line 27, you may think you are using the variable "norm", but since the variable has not been defined yet in line 27, MATLAB is trying to determine that norm is the name of the function, resulting in the error "Not enough input arguments".

请先登录,再进行评论。

采纳的回答

Scott MacKenzie
Scott MacKenzie 2021-5-18
编辑:Scott MacKenzie 2021-5-26
The first error is in the 3rd line. norm is a built-in MATLAB function and requires at least one input argument.
The second "error" (actually a "mistake") is on the 5th line in how you use the norm function. You correctly use norm with an input argument, but then you assign the result of the function to a variable named norm. This is actually allowed, but you need to realize that norm has now changed to a variable and is no longer a function. This creates an unintended side effect that will surface as an error if you use norm as a function later in your code.
Note that you also make the same mistake on the 1st line in your use of the mean function. It did not generate a syntax error from MATLAB, becuase this is (as I said) actually allowed. But, it is surely not what you intended.
Here's your code with these issues corrected:
mx = mean(x1); % assign the computed mean to a variable named mx
for i=1:length(x1)
x1(i) = (x1(i)-mx) / norm(x1); % correct use of norm function and use of variable mx
end
nx = norm(x1); % assign the computed norm to a variable named nx
for i=1:length(x1)
x1(i) = x1(i) / nx; % use of variable nx
end
  3 个评论
Scott MacKenzie
Scott MacKenzie 2021-5-18
The simplest way to see the values is to remove semi-colons at the end of lines. For example, change
mx = mean(x1);
to
mx = mean(x1)
When you run the scipt, the value is computed and sent to the Command Window.
You can get a bit fancy and print the value with some context, such as
fprintf('The mean is %f\n', mx);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by