In the description / help of matlab for the function var it says that the sum is normalized by default with N-1 instead of N that is why you get different results:

x = [1,1,1,2];

mean_x=mean(x);

var1=sum((x-mean_x).^2)/length(x)
var2=sum((x-mean_x).^2)/(length(x)-1)

var1 =

0.1875

var2 =

0.2500

You can find here a discussion about the difference of using N-1 rather than N. Var allows you to choose between both normalization by setting the second parameter to 0 default N-1 or 1 for N

Answer from Irreducible on Stack Overflow
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ data import and analysis โ€บ descriptive statistics and insights
var - Variance - MATLAB
This MATLAB function returns the variance of the elements of A along the first array dimension whose size does not equal 1.
Discussions

statistics - MATLAB var function returns weird results - Stack Overflow
I get different results when using MATLAB's var function, in compare to when calculating it based on the variance formula. My input is: x = [1,1,1,2]; when I use var function I get: var(x) ans ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Variance function in matlab
The variance function in matlab "var" by default normalizes V by N-1 if N>1, where N is the sample size If I want to normalize by the sampel size "N" NOT N-1, is it right to use: V = var(X,1)... More on mathworks.com
๐ŸŒ mathworks.com
1
0
June 16, 2019
what is var function in matlab?how does it works in image processing?
what is var function in matlab?how does it works... Learn more about what is var function in matlab? how does it works in image processing? More on mathworks.com
๐ŸŒ mathworks.com
1
0
November 7, 2013
Question About MATLAB Variance Built-in Function!
You seem to have compared the first and last elements. What if they happened to both be 0? Or both be 1, or one 0 and the other 1? The variance is the average of the deviation from the mean, which is what you've computed in the VAR , though you might subtract 1 from the len because its a sample variance. More on reddit.com
๐ŸŒ r/matlab
10
2
August 9, 2021
๐ŸŒ
MathWorks
mathworks.com โ€บ bioinformatics toolbox โ€บ microarray analysis โ€บ data import and management
var - Return variance values in DataMatrix object - MATLAB
This MATLAB function returns the variance values of the elements in the columns of a DataMatrix object, treating NaNs as missing values.
๐ŸŒ
MathWorks
mathworks.com โ€บ econometrics toolbox โ€บ multivariate models โ€บ vector autoregression models
Vector Autoregression (VAR) Models - MATLAB & Simulink
Pass a model template and data to estimate to obtain an estimated (fully specified) VAR model. Then, you can pass the estimated model to any other object function. Partially specified model template in which some parameters are known, and others are unknown and estimable. If you pass a partially specified model and data to estimate, MATLAB treats the known parameter values as equality constraints during optimization, and estimates the unknown values.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ programming โ€บ functions โ€บ argument definitions
varargin - Variable-length input argument list - MATLAB
If you use varargin to define an argument to an entry-point function, the code generator produces a C/C++ function with a fixed number of input arguments. You specify the number of arguments at the time of code generation. See Specify Number of Input or Output Arguments to Entry-Point Functions (MATLAB Coder).
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ language fundamentals โ€บ data types โ€บ tables
varfun - Apply function to table or timetable variables - MATLAB
This MATLAB function applies the function func separately to each variable of the table or timetable A and returns the results in the table or timetable B.
Find elsewhere
๐ŸŒ
MathWorks
mathworks.com โ€บ econometrics toolbox โ€บ regime-switching models โ€บ markov-switching dynamic regression models
varm - Create vector autoregression (VAR) model - MATLAB
The varm function returns a varm object specifying the functional form and storing the parameter values of a p-order, stationary, multivariate vector autoregression model (VAR(p)) model.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 467307-variance-function-in-matlab
Variance function in matlab - MATLAB Answers - MATLAB Central
June 16, 2019 - The variance function in matlab "var" by default normalizes V by N-1 if N>1, where N is the sample size If I want to normalize by the sampel size "N" NOT N-1, is it right to use: V = var(X,1)...
๐ŸŒ
Reddit
reddit.com โ€บ r/matlab โ€บ question about matlab variance built-in function!
r/matlab on Reddit: Question About MATLAB Variance Built-in Function!
August 9, 2021 -

Hey everyone,

I hope, everyone is doing well. I would like to ask question about matlab's var built-in function.

I just generated uniformly distributed random signal using rand.

%% noisy signal generation   

 noise=rand(len,1)'; 

%% variance formula for uniformly distributed signal 
%% var(signal)=((l-k)(l-k+2))/12; 

    param=noise(end)-noise(1); 
    variance=((param*(param+2)))/12;

%% this is how matlab calculate the var function 
    RESID = noise - mean(noise);
    VAR = sum(RESID.*conj(RESID)) / len;

When I tried to execute the variance calculation manually, I got the different result compared to built-in function. Basically results are for variance=0.0023 and VAR=0.0847.

I know that rand function generates numbers randomly between 0 and 1. But this difference is normal? Matlab built-in function normalizes the result. Can we assume that formula for the variance calculation already somehow normalized? ( I am not sure this is accurate to say!)

๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 37983-var-function
Var function - MATLAB Answers - MATLAB Central
May 10, 2012 - Plz how to use the output of the fuction var (to calculate the variance) to obtain the new image? Is there any formule? Thanks in advance ... Sign in to comment. Sign in to answer this question. Sign in to answer this question. Image Processing and Computer Vision Image Processing Toolbox Image Filtering and Enhancement Image Arithmetic ยท Find more on Image Arithmetic in Help Center and File Exchange ... Find the treasures in MATLAB Central and discover how the community can help you!
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ software engineering โ€บ how-to-calculate-variance-in-matlab
How to Calculate Variance in MATLAB? - GeeksforGeeks
July 23, 2025 - This is a very important quantity in statistics and many other fields like Machine Learning and AI. MATLAB provides a simple function to calculate the variance of a given data, the var() function.
๐ŸŒ
Matlab Scripts
matlabscripts.com โ€บ var-matlab
Understanding Var in Matlab: A Concise Guide
August 22, 2025 - How does `var` differ from `std`? While both functions measure dispersion, `var` calculates variance, and `std` computes the standard deviation, which is the square root of variance, providing a measure of spread in the same units as the data itself.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ programming โ€บ functions โ€บ argument definitions
varargout - Variable-length output argument list - MATLAB
varargout is an output variable in a function definition statement that enables the function to return any number of output arguments.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 473508-how-do-i-use-the-value-of-a-variable-in-function
How do I use the value of a variable in function? - MATLAB Answers - MATLAB Central
July 25, 2019 - How do I use the value of a variable in function?. Learn more about matrix, loop, if loop, for loop, matrix element, element insertion, insert element