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!
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 Exchange에서 Data 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