如何在 MATLAB 中計算方差?
在本文中,我們將探討**如何在 MATLAB 中計算方差**。在數學中,方差是一種統計工具,用於衡量一組資料點圍繞其平均值的離散程度。它被廣泛用於量化一組資料點的多樣性或可變性。
我們可以使用以下公式計算資料集的方差
$\mathrm{Var=\frac{\displaystyle\sum\limits_{i=1}^n (x_i −\bar{x})^2}{n}}$
其中,xi 是各個資料點,是資料集的平均值,n 是集合中資料點的總數。
本文的以下部分將透過示例 MATLAB 程式解釋如何計算資料集的方差。
在 MATLAB 中計算方差
MATLAB 提供了一個內建函式“var”來計算一組資料點的方差,以量化其圍繞其平均值的離散程度。“var”函式可以根據不同的用例具有幾種不同的語法。讓我們分別討論每種語法。
計算簡單方差
要計算一組資料點的方差,我們可以使用“var”函式的以下預設語法
Variance = var(A);
其中,A 是資料點的陣列或向量。
以下 MATLAB 程式演示了“var”函式預設語法的實現。
示例
% MATLAB code for calculating variance of a data set
% Create a vector of data points
A = [2, 4, 8, 10, 12, 16, 25];
% Calculate the variance of the data set
Variance = var(A);
% Display the original vector and its variance
disp('The input vector is:');
disp(A);
disp('Variance of the vector is:');
disp(Variance);
輸出
The input vector is:
2 4 8 10 12 16 25
Variance of the vector is:
60.333
程式碼解釋
在此 MATLAB 程式碼中,首先我們建立一個數據點向量“A”。然後,我們使用“var”函式計算向量“A”的方差。最後,我們使用“disp”函式顯示原始向量及其方差。
計算加權方差
我們使用“var”函式的以下語法來計算一組資料點的加權方差
Variance = var(A, w);
這裡,A 是資料點向量,w 是權重向量。
考慮以下 MATAB 程式以瞭解此語法的實現。
示例
% MATLAB code for calculating weighted variance of a data set
% Create a vector of data points
A = [2, 4, 8, 10, 12, 16, 25];
% Create a weight vector
w = [1, 2, 5, 4, 3, 7, 6];
% Calculate the weighted variance of the data set
Variance = var(A, w);
% Display the original vector and its variance
disp('The input vector is:');
disp(A);
disp('Weighted variance of the vector is:');
disp(Variance);
輸出
The input vector is:
2 4 8 10 12 16 25
Weighted variance of the vector is:
48.3367
程式碼解釋
在此 MATLAB 程式碼中,我們首先建立一個數據點向量“A”和一個權重向量“w”。需要注意的是,輸入向量和權重向量的尺寸必須相同。接下來,我們使用“w”作為第二個引數的“var”函式來計算向量“A”的加權方差。最後,我們使用“disp”函式顯示原始向量及其加權方差。
計算所有維度上的方差
“var”函式的以下語法用於計算資料集沿其所有維度的方差
Variance = var(A, 0, 'all');
考慮以下 MATLAB 程式以瞭解此語法的實現。
示例
% MATLAB code for calculating variance of a data set along all dimensions
% Create an array of data points
A = [2, 4, 8; 10, 12, 16; 25, 30, 35];
% Calculate the variance of the data set along all dimensions
Variance = var(A, 0, 'all'); % ‘0’ indicates sample variance
% Display the input array and its variance along all dimensions
disp('The input array is:');
disp(A);
disp('Variance of the array along all dimensions is:');
disp(Variance);
輸出
The input array is:
2 4 8
10 12 16
25 30 35
Variance of the array along all dimensions is:
136.6944
程式碼解釋
在此 MATLAB 程式碼中,我們首先定義一個數據點輸入陣列“A”。接下來,我們使用“var”函式使用“all”選項計算陣列“A”沿其所有維度的方差。最後,我們使用“disp”函式顯示輸入陣列及其沿所有維度的方差。
計算特定維度上的方差
“var”函式的以下語法用於計算資料集沿特定維度的方差
Variance = var(A, 0, dim);
這裡,如果 dim = 1,則沿陣列的行計算方差,如果 dim = 2,則沿陣列的列計算方差。
以下 MATLAB 程式說明了“var”函式此語法的實現。
示例
% MATLAB code for calculating variance of a data set along a specific dimension
% Create an array of data points
A = [2, 4, 8; 10, 12, 16; 25, 30, 35];
% Calculate the variance of the data set along rows
Variance_r = var(A, 0, 1);
% Calculate the variance of the data set along columns
Variance_c = var(A, 0, 2);
% Display the input array and its variance
disp('The input array is:');
disp(A);
disp('Variance of the array along rows is:');
disp(Variance_r);
disp('Variance of the array along columns is:');
disp(Variance_c);
輸出
The input array is:
2 4 8
10 12 16
25 30 35
Variance of the array along rows is:
136.3333 177.3333 192.3333
Variance of the array along columns is:
9.3333
9.3333
25.0000
程式碼解釋
在此 MATLAB 程式碼中,我們定義了一個數據點輸入陣列“A”。接下來,我們使用“var”函式分別計算陣列“A”的方差,其中“dim = 1”沿其行,“dim = 2”沿其列。最後,我們使用“disp”函式顯示輸入陣列及其沿行和列的方差。
結論
因此,這就是關於在 MATALB 中計算資料集方差的所有內容。MATLAB 提供了一個內建函式“var”來計算一組資料點的方差。在本文的上述部分中,我們解釋了“var”函式用於計算不同用例的方差的所有語法。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP