How to compute var on each matrix of a cell array?

6 views (last 30 days)
I have G = 1 * 227 cell and of each element has size 116 3. I and to compute variance of G e.g.,it should compute each matrix of G. I tried this way but not working
abc = var(G{K}) (if K = 1:227)
it gives e.g., abc = [a b c] onlyy three values.
How I can do it get

Answers (1)

Avni Agrawal
Avni Agrawal on 7 Feb 2024
Hi Nisar,
I understand that you are trying to compute the variance of each matrix within a cell array `G`, where `G` is a 1x227 cell array and each element of `G` is a 116x3 matrix.
The MATLAB function `var` computes the variance along each column of a matrix by default. If you apply `var` to each cell in the cell array using the syntax `var(G{K})`, it will give you a 1x3 vector for each matrix in the cell array, where each element of the vector is the variance of the corresponding column in the matrix. To compute the variance for each matrix in the cell array and store the results in a new array, you can use a loop or a cell function like `cellfun`. Here is how you can do it with a loop:
% Assuming G is a 1x227 cell array, with each cell containing a 116x3 matrix
variances = zeros(227, 3); % Preallocate a matrix to store the variances
for K = 1:227
variances(K, :) = var(G{K}); % Compute variance for each matrix and store it
end
If you want to compute the overall variance for each entire matrix (not column-wise), you would need to reshape each matrix into a vector and then compute the variance. Here is how you can do that:
% Assuming G is a 1x227 cell array, with each cell containing a 116x3 matrix
overall_variances = zeros(227, 1); % Preallocate a vector to store the overall variances
for K = 1:227
matrix_as_vector = G{K}(:); % Reshape the matrix into a vector
overall_variances(K) = var(matrix_as_vector); % Compute overall variance and store it
end
If you want to use `cellfun` to avoid explicit looping, it would look like this:
% For column-wise variance
variances = cellfun(@(x) var(x), G, 'UniformOutput', false);
variances = cell2mat(variances); % Convert cell array to a regular matrix
% For overall variance
overall_variances = cellfun(@(x) var(x(:)), G);
In the `cellfun` examples, `variances` will be a 227x3 matrix with the column-wise variances, and `overall_variances` will be a 227x1 vector with the overall variances of each matrix in the cell array `G`.
I hope this helps.
  4 Comments
Ahmed
Ahmed on 7 Feb 2024
Edited: Ahmed on 7 Feb 2024
@Stephen23 Thanks, it is working and then is this one correct?
N = 15
for K=1:227
C = cellfun(@var, G, 'UniformOutput', false);
VN = C{:,:}./N; % Is this correct divide all C with N like this? How I can divide a cell by N
stdN = sqrt(C./N);
ND = data{K,1} + stdN.*randn(size(data{K,1})); % the size of data{K,1} 116 227
end
Stephen23
Stephen23 on 7 Feb 2024
" it is working and then is this one correct?"
You are mixing up CELLFUN and a FOR-loop. You do not need both. Pick which one you want to use.
"Is this correct divide all C with N like this?"
No, it looks like you are tying to treat a cell array like a table. Cell arrays are not tables.
Both Avni Agrawal and I showed you how to use CELL2MAT. You should use CELL2MAT or a comma-separated list and e.g. VERTCAT:
vertcat(C{:})
"How I can divide a cell by N"
Cell arrays cannot be divided by anything.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!