How to extract number of matrices with all elements zero and the indexes of those matrices in a struct
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Voss
2023 年 1 月 16 日
% 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)
その他の回答 (1 件)
Paul
2023 年 1 月 16 日
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.
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!