如何在 MATLAB 中計算標準差?
在本文中,我們將學習如何在 **MATLAB 中計算標準差**。**標準差**是一種數學運算,用於衡量一組資料點的變化程度。標準差用於研究資料圍繞其平均值的離散程度。
如果一組資料點的標準差較低,則表示資料點傾向於更接近平均值。而標準差較高則表示資料點與平均值的離散程度較大。
在 MATLAB 中,我們可以使用名為“std()”的內建函式來計算標準差。根據不同的用例,“std”函式可以具有以下語法
陣列或向量的簡單標準差
“std”函式具有以下語法,用於計算向量或陣列的標準差。
S = std(A);
以下 MATLAB 程式演示了此語法在計算標準差中的用法。
示例
% MATLAB code for calculating simple standard deviation of a vector
% Create an input vector
A = [2, 4, 5, 7, 10];
% Calculate the standard deviation
S = std(A);
% Display the input vector and the standard deviation
disp('The input vector is:');
disp(A);
disp('Simple Standard Deviation of A is:');
disp(S);
輸出
The input vector is:
2 4 5 7 10
Simple Standard Deviation of A is:
3.0496
程式碼說明
在此 MATLAB 程式碼中,我們首先建立一個向量“A”。然後,我們使用“std”函式的預設語法計算標準差。最後,我們使用“disp”函式顯示輸入向量和結果。
帶權重的標準差
“std”函式的以下語法用於計算向量的帶權重標準差
S = std(A, w);
這裡,A 是輸入向量,w 是權重向量。
以下 MATLAB 程式演示了“std”函式此語法的實現。
示例
% Create an input vector and weight vector
A = [2, 4, 5, 7, 10];
w = [1, 2, 1, 0.5, 2]; % weight vector must have same number of elements as the vector A
% Calculate the weighted standard deviation
S = std(A, w);
% Display the input vector and the weighted standard deviation
disp('The input vector is:');
disp(A);
disp('Weighted Standard Deviation of A is:');
disp(S);
輸出
The input vector is:
2 4 5 7 10
Weighted Standard Deviation of A is:
2.9733
程式碼說明
在此 MATLAB 程式碼中,我們首先建立一個向量“A”。接下來,我們建立一個權重向量“w”。然後,我們使用“std”函式計算向量 A 的加權標準差。最後,我們使用“disp”函式顯示輸入向量和結果。
多維陣列所有元素的標準差
“std”函式的以下語法用於計算多維陣列所有元素的加權標準差
S = std(A, 0, 'all');
以下 MATLAB 程式顯示了“std”函式此語法的實現。
示例
% MATLAB program to calculate standard deviation of a multidimensional array
% Create a multidimensional array
A = [1, 2, 3; 5, 7, 7; 10, 13, 15];
% Calculating weighted standard deviation for all elements of A
S = std(A, 0, 'all');
% Display the input array and its standard deviation
disp('The input array is:');
disp(A);
disp('Standard Deviation for all elements is:');
disp(S);
輸出
The input array is:
1 2 3
5 7 7
10 13 15
Standard Deviation for all elements is:
4.8734
程式碼說明
在此 MATLAB 程式碼中,我們首先建立一個多維陣列“A”。然後,我們使用“std”函式和“all”選項計算陣列 A 所有元素的標準差。這裡,“std”函式中使用 w = 0 進行混合計算。最後,我們使用“disp”函式顯示輸入陣列和結果。
沿指定維度的陣列標準差
“std”函式使用以下語法計算陣列沿指定維度的標準差,即沿列或行。
S = std(A, w, dim);
如果 dim = 1,則將沿陣列的行計算標準差。如果 dim = 2,則將沿向量的列計算標準差。
以下 MATLAB 程式演示了“std”函式此語法的實現。
示例
% MATLAB program to calculate standard deviation of an array along a specific dimension
% Create an input array
A = [1, 2, 5; 6, 9, 7; 8, 4, 3];
% Create a weight vector
w = [1.5, 1, 2.5];
% Calculating standard deviation along the rows
S_r = std(A, w, 1);
% Calculating standard deviation along the columns
S_c = std(A, w, 2);
% Display the input array and the standard deviation along both dimensions
disp('The input array is:');
disp(A);
disp('Standard Deviation along rows:');
disp(S_r);
disp('Standard Deviation along columns:');
disp(S_c);
輸出
The input array is:
1 2 5
6 9 7
8 4 3
Standard Deviation along rows:
3.0414 2.4576 1.5620
Standard Deviation along columns:
1.8330
1.0440
2.1932
程式碼說明
在此 MATLAB 程式碼中,我們首先建立一個輸入陣列“A”。然後,我們建立一個權重向量“w”。接下來,我們使用“std”函式計算陣列 A 沿行 (dim = 1) 和沿列 (dim = 2) 的標準差。結果儲存在“S_r”和“S_c”變數中。最後,我們使用“disp”函式顯示輸入陣列和結果。
結論
因此,這就是使用 MATLAB 計算標準差的全部內容。MATLAB 提供了一個內建函式“std()”來計算陣列或向量的標準差。它針對不同的用例具有不同的語法。我們在本文的上述部分中藉助 MATLAB 程式解釋了“std”函式的所有這些語法。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP