Mean, Max and Min of Structured Cell array
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi all, I have a structured cell array where I need to compute the mean of each cell from column 4. Column 3 contains the name of the data (repeated for number of days 31, 29 etc.). At the end I would like to display the results as name (3rd column one value) , Average, Min and Max of each cell of 4th column data. Any help (sample example) in this regard is highly appreciated. For your reference I have attached the picture.

採用された回答
You should first convert everything to table form.
Column3={{'dog';'dog';'dog'},{'cat';'cat'}}; %Original data
Column4={[1;2;3], [4;5]};
Column3=string( vertcat(Column3{:}) );
Column4=vertcat( Column4{:} );
T=table(Column3,Column4,'VariableNames',{'Names','Data'})
T = 5×2 table
Names Data
_____ ____
"dog" 1
"dog" 2
"dog" 3
"cat" 4
"cat" 5
From that point on, it all becomes much easier:
Means = varfun(@mean,T,'GroupingVariables','Names')
Means = 2×3 table
Names GroupCount mean_Data
_____ __________ _________
"cat" 2 4.5
"dog" 3 2
Maxs= varfun(@max,T,'GroupingVariables','Names')
Maxs = 2×3 table
Names GroupCount max_Data
_____ __________ ________
"cat" 2 5
"dog" 3 3
14 件のコメント
Ganesh Naik
2021 年 4 月 8 日
Hi Matt, thanks for sample answer. Suppose Tc is the actual data could you please let me know how to access column 3 and 4 information (structured cell data from my example). I will go through your code in detail and try to work on it on my data. Thanks again
Matt J
2021 年 4 月 8 日
Column3=Tc(:,3);
Column4=Tc(:,4);
Ganesh Naik
2021 年 4 月 8 日
Hi Matt, thanks again. In Column 3 the variable "dog' is repeated 31 times etc. whereas in column 4, each cell has 31 or 29 data in it. In order to mimic your code do I need to use loop to extract the mean of each cell (mean from 31 samples from each cell)? Please let me know. But at least your example has given me some directions to work on this data. I will accept this answer once I achieve my result. Thanks and regards.
My example should work for you without modification.
Ganesh Naik
2021 年 4 月 9 日
Hi Matt I tried to use your method but it is not working for me. For my data, column 3 contains the same variable information "subject_ID1" for each cell (Subject_ID1 for 31 days) so I need to extract just one value for each cell in Column 3. For Column 4 I need to compute mean for each cell. For your reference I have attached a sample data.
Your Sample.mat is not a 4-column cell array like you originally posted. Regardless, once Column3 and Column4 are appropriately assigned, I'm seeing no problem with the output,
load(websave('Sample.mat', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/577972/Sample.mat'))
Column3=Sample.Subjects;
Column4=Sample.AHI;
Column3=string( vertcat(Column3{:}) );
Column4=vertcat( Column4{:} );
T=table(Column3,Column4,'VariableNames',{'Names','Data'});
Means = varfun(@mean,T,'GroupingVariables','Names')
Means = 5×3 table
Names GroupCount mean_Data
__________________ __________ _________
"001352tP2lnwGigq" 31 0.8345
"004fvTNq5S1rwmdy" 31 0.96764
"008LAKhG4nFZbD1h" 31 0.80035
"00X6LElMhyxcDLO3" 31 0.96794
"00cbPNUf6bSxKaiw" 31 0.93715
Ganesh Naik
2021 年 4 月 9 日
Hi Matt, thank you so much for the answer and help. Column 1 and Column 2 contains ID and Day information and for my anlysis it is not that much important. But now (with your code) I can modify it according to my requirement. I am going to accept this answer!!!
Thanks again.
Ganesh Naik
2021 年 4 月 10 日
Hi Matt, thanks for your help. Now, i am tryting to extract the one day (day1) information from the data (sample data attached) using "varfun" where I tried to create an indexing function using "day1=@(x) find(x==1);" but I am not able to get the indexed data. I am able to extract mean, min, max etc using inbuilt function but fail to extract the indexed data. Could you please guide me.
Shouldn't it be @(x) x(1) ?
Ganesh Naik
2021 年 4 月 10 日
Hi Matt I tried that as well, unfortunately that didnot work for me.
Matt J
2021 年 4 月 10 日
It works fine on our original toy example:
Column3={{'dog';'dog';'dog'},{'cat';'cat'}}; %Original data
Column4={[1;2;3], [4;5]};
Column3=string( vertcat(Column3{:}) );
Column4=vertcat( Column4{:} );
T=table(Column3,Column4,'VariableNames',{'Names','Data'})
T = 5×2 table
Names Data
_____ ____
"dog" 1
"dog" 2
"dog" 3
"cat" 4
"cat" 5
Firsts = varfun(@(x) x(1),T,'GroupingVariables','Names')
Means = 2×3 table
Names GroupCount Fun_Data
_____ __________ ________
"cat" 2 4
"dog" 3 1
Ganesh Naik
2021 年 4 月 10 日
Hi Matt, yes hence, I followed the same and other variations. Now, I need to extract the information from each subject for day 1 only. Hence, I tried to use find function to index "find(x==1)" method as well but it did not work for me.
Matt J
2021 年 4 月 10 日
But I just showed you that it works with @(x) x(1)
Ganesh Naik
2021 年 4 月 10 日
Hi Matt, thanks. I have managed to get it work.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Spreadsheets についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
