# Count percentage of certain number in struct

12 ビュー (過去 30 日間)
Nienke de Vries 2024 年 2 月 15 日

I have a 1x120 struct with 11 fields. 1 of the fields contains the condition (half of which 0, and half 1) and another field contains the accuracy (either good=1 or wrong=0).
I now want to know how many 1s are in the accuracy field for condition=1. How do I do this? Which function will I need?
I have multiple of these 1x120 structs, so I don't want to count it manually.
##### 2 件のコメントなしを表示なしを非表示
Dyuman Joshi 2024 年 2 月 15 日
nnz or sum with numel
Nienke de Vries 2024 年 2 月 15 日
thanks!

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

### 採用された回答

Voss 2024 年 2 月 15 日
% something similar to your 1x120 struct:
S = struct( ...
'condition',num2cell(logical(randi([0,1],1,120))), ...
'accuracy',num2cell(logical(randi([0,1],1,120))))
S = 1×120 struct array with fields:
condition accuracy
% number of elements of S with accuracy==1 and condition==1 (what you asked about)
n_good_condition_1 = nnz([S([S.condition]).accuracy])
n_good_condition_1 = 36
% number of elements of S with accuracy==1 and condition==0
n_good_condition_0 = nnz([S(~[S.condition]).accuracy])
n_good_condition_0 = 34
% number of elements of S with accuracy==0 and condition==1
n_wrong_condition_1 = nnz(~[S([S.condition]).accuracy])
n_wrong_condition_1 = 20
% number of elements of S with accuracy==0 and condition==0
n_wrong_condition_0 = nnz(~[S(~[S.condition]).accuracy])
n_wrong_condition_0 = 30
##### 2 件のコメントなしを表示なしを非表示
Nienke de Vries 2024 年 2 月 15 日
Thank you so much for this extensive answer!!
Voss 2024 年 2 月 15 日

You're welcome! Any questions, let me know.

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

### その他の回答 (2 件)

Adam Danz 2024 年 2 月 15 日

Looks like @Voss hit the nail on the head.
Another approach is to use groupcounts to tally the counts between the two groups.
1. Convert the structure to a table. I'll use Voss's struct example.
S = struct( ...
'condition',num2cell(logical(randi([0,1],1,120))), ...
'accuracy',num2cell(logical(randi([0,1],1,120))));
T = struct2table(S);
head(T) % show some rows of the table
condition accuracy _________ ________ false false true false false true true false true false false false false true false true
2. Apply groupcounts
summaryTable = groupcounts(T,{'condition','accuracy'})
summaryTable = 4×4 table
condition accuracy GroupCount Percent _________ ________ __________ _______ false false 32 26.667 false true 38 31.667 true false 27 22.5 true true 23 19.167
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

Austin M. Weber 2024 年 2 月 15 日
You can use nnz which tells you how many non-zeros are in an array. If your struct is named s then you can extract the data you want and perform the calculations:
idx = s.Condition == 1; % Find positions where the condition = 1
accuracy = s.Accuracy(idx); % Find the accuracy values where the condition = 1
number_of_ones = nnz(accuracy); % Count number of ones in 'accuracy'
##### 4 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
Austin M. Weber 2024 年 2 月 15 日
@Voss Thank you for the clarification!
Voss 2024 年 2 月 15 日
@Austin M Weber You're welcome!

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

### カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by