Variance calculation with Matlab [duplicate] - matlab

This question already has answers here:
How to calculate sample and population variances in Matlab?
(2 answers)
Closed 6 years ago.
I don't understand why MATLAB does not normalize the data by its size while computing the variance. Ex :
a=[1 2 3];
var(a); %// 1
However we know that the fundamental definition of the variance is :
variance(a) = ((a(i)-mean(a))^2) / size(a) %// here size(a)=3
%//this formula gives a variance equal to 2/3
any idea?

It's explained in the docs:
MATLAB is uses the formula for unbiased sample variance by default

Related

How to speed up column wise operation in MATLAB using bsxfun? [duplicate]

This question already has answers here:
Fast Algorithms for Finding Pairwise Euclidean Distance (Distance Matrix)
(3 answers)
Closed 5 years ago.
I am trying to calculate the Square Euclidean Distance between each column from two matrices and store in matrix D.
im_patches is 81*60840 double
codebook is 81*456 double
SquareEuclidean = #(x, y) x'*x+y'*y-2*x'*y;
% Get N*K distance matrix D between the N patches extracted
% from the image (im patches) and the K prototypes in the codebook
D=zeros(size(im_patches,2),size(codebook, 2));
for i=1:size(im_patches,2)
for j=1:size(codebook, 2)
D(i,j)=SquareEuclidean(im_patches(:,i),codebook(:,j));
end
end
However, this is very inefficient that cost more than 10 minutes in my laptop.
I am wondering is there a better way of using bsxfun. So I tried:
D2 = bsxfun(#(x,y) x'.*x+y'.*y-2.*x'.*y,im_patches,codebook);
which gives an error:
Error using bsxfun: Non-singleton dimensions of the two input arrays must match each other.
I think bsxfun or arrayfun would be a nice way of dealing such problem. But don't know the correct way of doing this.
Thank you in advance.
Your loop can be reduced to:
bsxfun(#plus,sum(im_patches.'.^2,2),sum(codebook.^2)-2*im_patches.'*codebook)
In MATLAB r2016b there is no need to bsxfun:
sum(im_patches.'.^2,2)+sum(codebook.^2)-2*im_patches.'*codebook

How to choose the ndim parameter in pcares function in matlab and reduce the dimensionality of data? [duplicate]

This question already has answers here:
How many principal components to take?
(6 answers)
how to check whether the image is compressed or not after applying SVD on that image(regarding size of compressed image on disk)
(2 answers)
Dimensionality reduction in Matlab
(1 answer)
How to use eigenvectors obtained through PCA to reproject my data?
(1 answer)
What does selecting the largest eigenvalues and eigenvectors in the covariance matrix mean in data analysis?
(1 answer)
Closed 5 years ago.
I have a dataset of size 200*119 i.e. my samples are 200 and the variables/features are 119. I want to use PCA to optimize my feature set by selecting only those features that contribute significantly to classification.
I have understood the concept of PCA but am unable to implement it. I have found out the coeff and score of my data using the pca function.
[coeff, score] = pca(data);
The coeff matrix is of size 119x119 now.
But what do I do with this information? My goal is to find the reduced dataset that can be fed into a classifier. I have gone through the documentation for pcares and even looked at similar questions posted regarding this issue. But I am unable to understand how [residuals, reconstructed]=pcares(data, ndim) will help me "reduce" the size of my dataset. How do I go about choosing ndim parameter?
EDIT
I used the following code to reduce dataset.
B=data;
sigma = cov(B);
%// Find eigenvalues and eigenvectors of the covariance matrix
[A,D] = eig(sigma);
vals = diag(D);
%// Sort their eigenvalues
[~,ind] = sort(abs(vals), 'descend');
%// Rearrange eigenvectors
Asort = A(:,ind);
%// Find mean subtracted data
Bm = bsxfun(#minus, B, mean(B,1));
%// Reproject data onto principal components
Bproject = Bm*Asort;
However, my Bproject is still of the size 200*119
I do not understand this. Please explain.

How to assign vector elements to get a square wave? [duplicate]

This question already has answers here:
How to have square wave in Matlab symbolic equation
(3 answers)
Closed 6 years ago.
I have a vector T that is defined as
T=zeros(1,4)
I want to define T such that T(1) and T(2) are equal to 1 and T(3) and T(4) are equal to 0. So that when I plot T it looks like a square wave.
I have tried
for i=1:2:size(T,2)
T(i:i+1)=1
end
figure; plot(T);
But this does not give the desired result. It turns out to be [1,0,1,0].
What is the right way to do this assignment?
To differentiate from questions about plotting square waves:
I wanted to find out how exactly to create the loop that would plot to look like a square wave, without explicitly defining frequency or using the symbolic equation. I would then use this information to modify another script that would do the same thing but a larger vector T where the "period" is not the same. Sometimes it is 11s, sometimes 9s and so on.
The period is 4, not 2:
for i=1:4:size(T,2)
T(i:i+1)=1
end
figure; plot(T);
If you have access to the signal processing toolbox, an alternative is using the square function:
T = (1+square(0:pi/2:3*pi/2))/2 %// 1 1 0 0

applying norm function to rows of matrix - Matlab [duplicate]

This question already has answers here:
Vector norm of an array of vectors in MATLAB
(4 answers)
Closed 5 years ago.
I have a 3 columns, n rows matrix:
[ a,b,c;
d,e,f;
g,h,i; ]
I want to apply the norm function to each of the rows, and get a 1xn matrix containing the norms:
[ norm([a,b,c]);
norm([d,e,f]);
norm([g,h,i]); ]
I could do this with a for-loop, but is there a better way?
What about
norms = sqrt(sum(A.^2,1))
or
norms = sqrt(sum(A.^2,2))?
depending on whether your coordinates are in rows or in columns.
If readability is a bigger consideration than performance you might also consider:
norms = cellfun(#norm,num2cell(A,2));
This pattern is also adaptable to other operations along one dimension you might want to perform where MATLAB doesn't support it natively.
if the first dimension is not too large:
norms = sqrt(diag(A * A'));

how to distribute probability randomly for generating test data in matlab? [duplicate]

This question already has answers here:
Generating N numbers that sum to 1
(6 answers)
Closed 10 years ago.
How do I distribute probability randomly over n values in matlab?
If I have 128 vectors.
I want to assign a random probabilty to all of them such that the sum of all of them equals to 1.
e.g.
n=4
p1=0.37
p2=0.21
p3=0
p4=0.42
Depending on how random you need to be, Roger Stafford takes a more stringent approach.
You can just divide the vector by the sum of it's elements. For example, for a vector of length 4 you can do:
>> v = rand(4, 1);
>> v = v/sum(v)
v =
0.2951
0.3281
0.0460
0.3308
>> sum(v)
ans =
1.0000
Note, I am assuming you want uniformly distributed numbers, since you don't state what distribution you want in the question.