MATLAB Answers

Function to load a .mat file (which can be inside or outside matlab folder) and perform analysis for finding constant or near-zero variance values

2 ビュー (過去 30 日間)
HT
HT 2018 年 2 月 4 日
コメント済み: Walter Roberson 2018 年 2 月 7 日
I have a matlab data file that has only one variable of type 1x1 struct which has 1x680 cell which has 680 MxN double values (values can be -ve, 0 or +ve). I want to load this data file using a function and find constant and near-zero variance values in this data set.
Thanks in advance.

  0 件のコメント

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2018 年 2 月 4 日
[filename, pathname] = uigetfile('*.mat', 'Selected mat file');
if isnumeric(filename); return; end %user cancel
filename = fullfile(pathname, filename);
datastruct = load(filename);
varnames = fieldnames(datastruct);
datacell = datastruct.(varnames{1}); %there should only _be_ one, but just in case there are more
variances = cellfun(@(M) var(M(:)), datacell);
has_small_variance = abs(variances) < 1e-5;
selected_cells = datacell(has_small_variance);

  7 件のコメント

表示 4 件の古いコメント
Walter Roberson
Walter Roberson 2018 年 2 月 6 日
var() by default works one column at a time, but you want the variance over the entire matrix within each cell, so we run a function on each cell that reshapes the content of the cell into a vector using the (:) operator, and then takes the variance of the resulting vector. That is a single numeric value for each cell entry so you do not need 'uniform', 0 in the cellfun call.
If you wanted to take the variance per column, then you would
cellfun(@var, datacell, 'uniform', 0)
but then you would need to define more clearly what it means for hte matrices to be similar in variance.
HT
HT 2018 年 2 月 7 日
Thanks.
Is it possible also to calculate other values, lets say mean of the cells in this code. And for specifying the threshold (variances<some value), is it possible to give control to user for specifying the value.
Walter Roberson
Walter Roberson 2018 年 2 月 7 日
tol = input('What tolerance do you want?');
means = cellfun(@mean2, datacell);
variances = cellfun(@(M) var(M(:)), datacell);
has_small_variance = abs(variances) < tol;
selected_cells = datacell(has_small_variance);
selected_variances = variances(has_small_variance);
selected_means = means(has_small_variance);

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by