Mean, Max and Min of Structured Cell array
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
Column3=Tc(:,3);
Column4=Tc(:,4);
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.
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
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.
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) ?
Hi Matt I tried that as well, unfortunately that didnot work for me.
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
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)
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
