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!