在执行关系及逻辑运算时,MATLAB 将输入的不为零的数值都视为真 (True)而为零的数值则视为否 (False)。 运算的输出值将判断为真者以 1表示 而判断为否者以 0 表示。MATLAB 提供以下的关系判断及逻辑的运算 元:
符号 关系的意义
[code]< 小于
<= 小于等于
> 大于
>= 大于等于
== 等于
~= 不等于
& 逻辑 and
| 逻辑 or
~ 逻辑 not[/code]
上述的各个运算元须用在二个大小相同的阵列或是矩阵的比较
使用逻辑运算产生一个不连续的信号:
[code]x=linspace(0,10,100); % 产生数据
y=sin(x); % 产生 sine 函数
z=(y>=0).*y; % 将 sin(x) 的负值设为零
z=z + 0.5*(y<0); % 再将上式的值加上0.5
z=(x<8).*z; % 将大于 x=8 以后的值设为零
hold on
plot(x,z)
xlabel('x'),ylabel('z=f(x)')
title('A discontinuous signal')
hold off[/code]
执行结果参看luoji_.png
逻辑关系函数:xor(x,y), any(x), all(x), isnan(x), isinf(x), finite(x), find(x)
查看find的解释:
[quote]>> help find
FIND Find indices of nonzero elements.
I = FIND(X) returns the indices of the vector X that are
non-zero. For example, I = FIND(A>100), returns the indices
of A where A is greater than 100. See RELOP.
[I,J] = FIND(X) returns the row and column indices of
the nonzero entries in the matrix X. This is often used
with sparse matrices.
[I,J,V] = FIND(X) also returns a vector containing the
nonzero entries in X. Note that find(X) and find(X~=0)
will produce the same I and J, but the latter will produce
a V with all 1's.[/quote]
符号 关系的意义
[code]< 小于
<= 小于等于
> 大于
>= 大于等于
== 等于
~= 不等于
& 逻辑 and
| 逻辑 or
~ 逻辑 not[/code]
上述的各个运算元须用在二个大小相同的阵列或是矩阵的比较
使用逻辑运算产生一个不连续的信号:
[code]x=linspace(0,10,100); % 产生数据
y=sin(x); % 产生 sine 函数
z=(y>=0).*y; % 将 sin(x) 的负值设为零
z=z + 0.5*(y<0); % 再将上式的值加上0.5
z=(x<8).*z; % 将大于 x=8 以后的值设为零
hold on
plot(x,z)
xlabel('x'),ylabel('z=f(x)')
title('A discontinuous signal')
hold off[/code]
执行结果参看luoji_.png
逻辑关系函数:xor(x,y), any(x), all(x), isnan(x), isinf(x), finite(x), find(x)
查看find的解释:
[quote]>> help find
FIND Find indices of nonzero elements.
I = FIND(X) returns the indices of the vector X that are
non-zero. For example, I = FIND(A>100), returns the indices
of A where A is greater than 100. See RELOP.
[I,J] = FIND(X) returns the row and column indices of
the nonzero entries in the matrix X. This is often used
with sparse matrices.
[I,J,V] = FIND(X) also returns a vector containing the
nonzero entries in X. Note that find(X) and find(X~=0)
will produce the same I and J, but the latter will produce
a V with all 1's.[/quote]