MATLAB關係運算
關係運算符標和非標量數據上也能正常工作。關係運算符對數組進行元素元素元素設置為邏輯1(真)的關係是真實的和元素設置為邏輯0(假),它是兩個陣列,並返回一個同樣大小的邏輯陣列之間的比較。
以下的表格顯示的關係運算符:
操作符 | 描述 |
---|---|
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
== | Equal to |
~= | Not equal to |
例子
創建一個腳本文件,並鍵入下麵的代碼:
a = 100; b = 200; if (a >= b) max = a else max = b end
當運行該文件,它會產生以下結果:
max = 200
除了在上述的關係運算符,MATLAB提供下列命令/函數用於同樣的目的:
函數 | 描述 |
---|---|
eq(a, b) | Tests whether a is equal to b |
ge(a, b) | Tests whether a is greater than or equal to b |
gt(a, b) | Tests whether a is greater than b |
le(a, b) | Tests whether a is less than or equal to b |
lt(a, b) | Tests whether a is less than b |
ne(a, b) | Tests whether a is not equal to b |
isequal | Tests arrays for equality |
isequaln | Tests arrays for equality, treating NaN values as equal |
例子
創建一個腳本文件,並鍵入下麵的代碼:
% comparing two values a = 100; b = 200; if (ge(a,b)) max = a else max = b end % comparing two different values a = 340; b = 520; if (le(a, b)) disp(' a is either less than or equal to b') else disp(' a is greater than b') end
當運行該文件,它會產生以下結果:
max = 200 a is either less than or equal to b