MATLAB教程系列 台大 03 exercise

2021-08-23 12:25:08 字數 1221 閱讀 5200

第三課中的練習,溫度轉換。要求,除非輸入的不是數字,程式停止,否則一直提示輸入溫度。

function y = ftoc (f)

tips1 = 'temperature in f is: ';

f = input (tips1);

while ( (isnumeric(f) == 1) ) && (isempty(f) == 0)

% || (isempty(str2num(f)) == 0)

% 第五行這一方法可以用於判斷輸入是否為字元,但此程式中沒有必要一定加這個條件

c = (f-32) .* (5/9);

disp(['temperature in c is: ',num2str(c)]);

tips1 = 'temperature in f is: ';

f = input (tips1);

end

輸出結果:

1. 

>>ftoc

temperature in f is: 32

temperature in c is: 0

temperature in f is: 45

temperature in c is: 7.2222

temperature in f is: 12

temperature in c is: -11.1111

temperature in f is:

2. if we continue to input a character:

temperature in f is: 'k'

>>

(the program stops)

3. input the space or press 'enter'

>> ftoc

temperature in f is:

>>

(the program stops)

附上對str2num(f)的使用說明:

如果f為乙個字元,則:

>> str2num('k')

ans =

>> str2num('abc')

ans =

可以看到,如果輸入為字元,則返回乙個空矩陣,因此可以通過這種方法來判斷輸入是否為字元。

>> isempty(str2num('abc'))

ans =

logical

MATLAB教程系列 台大 09 exercise

1.不通過im2bw 函式完成影象二值化 通過graythreshold 函式,程式可以自動找到乙個對於當前來說更加合適的閾值來進行二值化。通過im2bw 影象,可以完成二值化,把當前影象變為只有黑白兩色的。例程如下 i imread rice.png level graythresh i bw i...

Matlab系列教程 數值計算 求方差和標準差

首先,什麼是方差和標準差?方差,是在概率論和統計方差衡量隨機變數或一組資料時離散程度的度量,統計中的方差 樣本方差 是每個樣本值與全體樣本值的平均數之差的平方值的平均數。在許多實際問題中,研究方差即偏離程度有著重要意義。標準差,中文環境中又常稱均方差,是離均差平方的算術平均數的平方根。標準差是方差的...

Matlab系列教程之數值計算求方差和標準差

首先,什麼是方差和標準差?方差,是在概率論和統計方差衡量隨機變數或一組資料時離散程度的度量,統計中的方差 樣本方差 是每個樣本值與全體樣本值的平均數之差的平方值的平均數。在許多實際問題中,研究方差即偏離程度有著重要意義。標準差,中文環境中又常稱均方差,是離均差平方的算術平均數的平方根。標準差是方差的...