Hi. I am working with a structure array S (1 X 20,000) with 3 fields. I want to count the number of S(i) that meet a condition on one of its fields. For example,
Here is the input,
S(1).f1=[11,17,3,18,15,13], S(1).f2=[100,20,50,60,70,140] and S(1).f3=[-10,20,-50,42,-70,140] ;
S(2).f1=[10,12,14,17,19], S(2).f2=[101,54,69,20,11] and S(2).f3=[17,-54,69,-20,37];
S(3).f1=S(1).f1=[19,17,13,14,15,10,11,16], S(3).f2=..... and S(3).f3=...........;
S(4).f1=[11,17,30,108,15,13,37,14], , S(4).f2=..... and S(4).f3=............;
.
.
S(i).f1=...., S(i).f2=.... and S(i).f3=............;
Let's say, I have a condition on f1: 10 < f1 <=20. Based, on this condition I want the count of S(i) whose f1 is strictly in the these limits. In this example, S(2).f1 and S(3).f1 has all the f1 in bewteen 10 and 20, the count is 2.
I want to implement this on S (1 X 20,000). Can someone help me with this?
Thanks, in advance.

 採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 10 日

0 投票

count = nnz(arrayfun(@(S) all(10 < S.f1 & S.f1 <= 20), S))

3 件のコメント

SS
SS 2020 年 6 月 10 日
編集済み: SS 2020 年 6 月 10 日
Thank you so much. How, can I find the number of elements in S(2).f1 and S(3).f1? I am also interested in calculating the mean and variance of the corresponding f2 elements meeting the above condition? Meaning, mean and variance of S(2).f2 and S(3).f2?
Any help is highly appreciated!
Walter Roberson
Walter Roberson 2020 年 6 月 10 日
mask = arrayfun(@(s) all(10 < s.f1 & s.f1 <= 20), S);
count = nnz(mask);
selected_f1_element_counts = cellfun(@numel, {S(mask).f1});
selected_f2 = {S(mask).f2};
selected_f2_means = cellfun(@mean, selected_f2);
selected_f2_variances = cellfun(@var, selected_f2);
SS
SS 2020 年 6 月 10 日
Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Types についてさらに検索

質問済み:

SS
2020 年 6 月 9 日

コメント済み:

SS
2020 年 6 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by