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.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 10 Jun. 2020

0 Stimmen

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

3 Kommentare

SS
SS am 10 Jun. 2020
Bearbeitet: SS am 10 Jun. 2020
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!
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 am 10 Jun. 2020
Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Types finden Sie in Hilfe-Center und File Exchange

Gefragt:

SS
am 9 Jun. 2020

Kommentiert:

SS
am 10 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by