matlab when函数
时间: 2023-11-22 13:05:11 浏览: 283
在MATLAB中,"when"函数用于在给定条件为真时执行特定的操作。它的语法如下:
```matlab
when(condition, action)
```
其中,condition是一个逻辑表达式,用于判断是否执行action操作。如果condition为真(非零),则执行action。否则,当condition为假(零)时,不执行任何操作。
例如,假设我们要根据某个变量x的值来执行不同的操作,可以使用"when"函数来实现。下面是一个简单的示例:
```matlab
x = 5;
when(x > 0, disp('x is positive.'))
when(x < 0, disp('x is negative.'))
when(x == 0, disp('x is zero.'))
```
在这个示例中,当x的值为正时,将会显示"x is positive.";当x的值为负时,将会显示"x is negative.";当x的值为零时,将会显示"x is zero."。根据不同的条件,执行相应的操作。
请注意,在使用"when"函数时,条件表达式必须返回逻辑值(真或假)。如果条件表达式返回其他类型的值,将会引发错误。
希望这个回答能够帮助到你!如果有任何进一步的问题,请随时提问。
相关问题
显示MATLAB filter函数的源码
MATLAB 的 filter 函数的源码如下:
```matlab
function [y,zf] = filter(b,a,x,zi,dim)
%FILTER 1-D digital filter.
% Y = FILTER(B,A,X) filters the data in vector X with the filter described by
% vectors A and B to create the filtered data Y. The filter is a "Direct
% Form II Transposed" implementation of the standard difference equation:
%
% a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)
% - a(2)*y(n-1) - ... - a(na+1)*y(n-na)
%
% The initial states of the filter are assumed to be zero.
%
% If A(1) is not equal to 1, FILTER normalizes the filter coefficients by A(1).
%
% Y = FILTER(B,A,X,Z) gives access to initial and final conditions, Z. If Z
% is a scalar, FILTER applies the IC to both the input and output data. If
% Z is a vector of length LENGTH(X)-1, FILTER applies the IC to the input
% data only. If Z is the empty matrix [], FILTER applies no IC.
%
% Y = FILTER(B,A,X,[],DIM) or Y = FILTER(B,A,X,Z,DIM) operates along the
% dimension DIM. The DIM'th dimension of X must have length greater than 1.
%
% [Y,ZF] = FILTER(B,A,X,Z,...) returns the final conditions, ZF. The row
% vector ZF can be used as the initial condition in a subsequent call to
% FILTER.
%
% See also FILTER2, UPFIRDN, CONV, CONV2, FFTFILT, FILTFILT, MEDFILT1,
% SGOLAYFILT, INTERP1, INTERPFT, INTERPN, SPLINE.
% Filter Designer GUI: FDESIGN.
%
% Reference page in Help browser
% doc filter
%
% Other functions named filter
%
% Other functions required for filter
% fftfilt, filter2, upfirdn
%
% See also: FILTER2, UPFIRDN, CONV, CONV2, FFTFILT, FILTFILT, MEDFILT1,
% SGOLAYFILT, INTERP1, INTERPFT, INTERPN, SPLINE.
% Copyright 1984-2019 The MathWorks, Inc.
% Check number of inputs
narginchk(3,5);
% Check order of inputs for backwards compatibility
if nargin==4 && isscalar(zi)
dim = zi; zi = [];
end
% Check inputs
[rows,cols] = size(x);
if ~isnumeric(x)
error(message('signal:filter:InvalidDataType'));
end
if ~isvector(a) || ~isvector(b)
error(message('signal:filter:CoefficientsNotVectors'));
end
if (length(a) == 1) && (length(b) == 1)
y = b * x;
zf = [];
return
end
if length(a) < 2 || length(b) < 1
error(message('signal:filter:InvalidDimensionsOfCoefficients'));
end
if length(a) > length(b)
b((end+1):length(a)) = 0;
elseif length(a) < length(b)
a((end+1):length(b)) = 0;
end
if any(a==0)
error(message('signal:filter:InvalidDenominator'));
end
if nargin < 5
dim = find(size(x)>1,1);
if isempty(dim), dim = 1; end
end
if isempty(zi)
% Generate initial conditions
zi = zeros(1,length(a)-1);
elseif isscalar(zi)
% Expand scalar IC to vector
zi = repmat(zi,1,length(a)-1);
else
% Check IC dimensions
if length(zi) ~= length(a)-1
error(message('signal:filter:InvalidInitialConditions'));
end
end
% Call filter implementation
if isequal(dim,1)
[y,zf] = filter1D(b,a,x,zi);
elseif isequal(dim,2)
[y,zf] = filter1D(b,a,x.',zi);
y = y.';
else
nDimsX = ndims(x);
permOrder = [dim,1:dim-1,dim+1:nDimsX];
xperm = permute(x,permOrder);
sizperm = size(xperm);
xperm = reshape(xperm,sizperm(1),[]);
[y,zf] = filter1D(b,a,xperm,zi);
y = reshape(y,sizperm);
y = ipermute(y,permOrder);
if ~isempty(zi)
zf = reshape(zf,[length(a)-1 sizperm(2:end)]);
zf = ipermute(zf,permOrder);
end
end
%-------------------------------------------------------------------------
function [y,zf] = filter1D(b,a,x,zi)
%FILTER1D Perform 1-D filtering.
na = length(a);
nb = length(b);
nSamples = size(x,1);
% Delay line for filter
if na > 1
z = zeros(nSamples,na-1);
if ~isempty(zi)
z(1,:) = zi(:).';
end
else
z = [];
end
% Allocate output
if isreal(x) && all(isfinite(x(:))) && all(isfinite(b(:))) && ...
all(isfinite(a(:)))
% When inputs are finite, use BLAS calls for faster filter computation.
% Use real arithmetic when inputs and coefficients are real.
y = zeros(size(x),class(x));
if na > 1
for k = 1:nSamples
if nb > 1
y(k) = b(1)*x(k) + sum(b(2:end).*z(k,:));
z(k,:) = [y(k) z(k,1:end-1)];
else
y(k) = b*x(k);
end
if na > 1
y(k) = y(k) - sum(a(2:end).*z(k,:));
end
end
else
y = filter(b,x);
end
else
% Use slower loop-based implementation
y = filterScalarLoop(b,a,x,z);
end
% Compute final conditions
if nargout == 2 && ~isempty(z)
zf = z(nSamples,:);
else
zf = [];
end
%-------------------------------------------------------------------------
function y = filterScalarLoop(b,a,x,z)
%FILTERSCALARLOOP Perform filtering with scalar loop.
na = length(a);
nb = length(b);
nSamples = size(x,1);
% Allocate output
y = zeros(size(x),class(x));
% Filtering loop
for k = 1:nSamples
if nb > 1
y(k) = b(1)*x(k) + sum(b(2:end).*z(k,:));
z(k,:) = [y(k) z(k,1:end-1)];
else
y(k) = b*x(k);
end
if na > 1
y(k) = y(k) - sum(a(2:end).*z(k,:));
end
end
```
matlb中when函数的使用方法
在MATLAB中,`when`函数是一个控制流语句,用于在条件为真时执行一系列语句。其语法如下:
```
when condition
statements
end
```
其中,`condition`是一个逻辑表达式,可以是任何产生逻辑结果的表达式。
当`condition`为真时,`when`块中的`statements`会被执行。如果`condition`为假,则`when`块中的内容会被跳过。
需要注意的是,`when`语句只会执行一次。如果`condition`为真,`statements`被执行后,程序会跳过整个`when`块并继续执行后续的代码。
以下是一个使用`when`语句的例子:
```
x = 10;
when x == 10
disp('x is 10');
x = x + 1;
end
disp(x);
```
在这个例子中,当`x`等于10时,`when`块中的语句会被执行,输出`x is 10`并将`x`的值增加1。最后,整个程序输出`11`。
阅读全文