Length of the structure array

10 views (last 30 days)
SS
SS on 9 Jun 2020
Commented: SS on 10 Jun 2020
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.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2020
count = nnz(arrayfun(@(S) all(10 < S.f1 & S.f1 <= 20), S))
  3 Comments
Walter Roberson
Walter Roberson on 10 Jun 2020
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 on 10 Jun 2020
Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!