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

採用された回答

Voss
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 = struct with fields:
grid: [12×6 struct]
gridcell_meancov(1).grid
ans = 12×6 struct array with fields:
mean cov
% obtain which "cov" matrices are all zero:
cov_all_zero = ~cellfun(@(x)any(x,'all'),{gridcell_meancov(1).grid.cov})
cov_all_zero = 1×72 logical array
1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
% the number of all-zero cov matrices:
n_all_zero = nnz(cov_all_zero)
n_all_zero = 7
% the indexes of those all-zero cov matrices:
idx_all_zero = find(cov_all_zero)
idx_all_zero = 1×7
1 4 14 31 49 59 60
% the number of not-all-zero cov matrices:
n_not_all_zero = nnz(~cov_all_zero)
n_not_all_zero = 65
% the indexes of those not-all-zero cov matrices:
idx_not_all_zero = find(~cov_all_zero)
idx_not_all_zero = 1×65
2 3 5 6 7 8 9 10 11 12 13 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 32 33 34

その他の回答 (1 件)

Paul
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')
ans = 2
If I've misinterpreted the arrangment of the data, then post a small example that illustrates the data you have.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by