How to extract number of matrices with all elements zero and the indexes of those matrices in a struct
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi
I want to obtain the number of zero or nonzero matrices and their indexes in a struct like in the figure on the left. I am trying to check the cov named matrix in the right figure which are in all 1x1 struct cells on the left figure.
Thank you
0 commentaires
Réponse acceptée
Voss
le 16 Jan 2023
% making up some data like yours:
gridcell_meancov = struct('grid',struct('mean',num2cell(rand(12,6)),'cov',squeeze(num2cell(randi([0 1],[2 2 12 6]),[1 2]))))
gridcell_meancov(1).grid
% obtain which "cov" matrices are all zero:
cov_all_zero = ~cellfun(@(x)any(x,'all'),{gridcell_meancov(1).grid.cov})
% the number of all-zero cov matrices:
n_all_zero = nnz(cov_all_zero)
% the indexes of those all-zero cov matrices:
idx_all_zero = find(cov_all_zero)
% the number of not-all-zero cov matrices:
n_not_all_zero = nnz(~cov_all_zero)
% the indexes of those not-all-zero cov matrices:
idx_not_all_zero = find(~cov_all_zero)
2 commentaires
Plus de réponses (1)
Paul
le 16 Jan 2023
Hi Abdullah,
The Question use the term "cell" but it looks like the data only uses structures. If that's the case, then ....
Create some example data
gridcell_meancov(1).grid(1,1).mean = 0;
gridcell_meancov(1).grid(1,1).cov = zeros(2);
gridcell_meancov(1).grid(1,2).mean = 0;
gridcell_meancov(1).grid(1,2).cov = eye(2);
gridcell_meancov(1).grid(2,1).mean = 0;
gridcell_meancov(1).grid(2,1).cov = zeros(2);
gridcell_meancov(1).grid(2,2).mean = 0;
gridcell_meancov(1).grid(2,2).cov = eye(2);
The result
sum(arrayfun(@(S) all(S.cov==0,'all') ,gridcell_meancov(1).grid),'all')
If I've misinterpreted the arrangment of the data, then post a small example that illustrates the data you have.
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!