Cell array: How can I calculate the mean of it?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
I got an cell array. You can find it attached.
time1, etc. are the times of the different testing. And values are the meassured values according to the times. So values 1 is connected with time1 and values2 is connected with time2
I would like to get the mean of the "values1" if the conditions "time1>0 & time<=3" is fulfilled.
So the solution would be (4+6+3)/3=4.33333
How can I do this?
Thanks.
댓글 수: 3
Image Analyst
2019년 6월 30일
Make it easy for people to help you, not hard, by attaching your cell array in a .mat file. In the meantime, check out cellfun(), cell2mat(), and mean().
채택된 답변
KALYAN ACHARJYA
2019년 6월 30일
편집: KALYAN ACHARJYA
2019년 6월 30일
load test_file.mat
idx=find(time1>0 & time1<=4);
result=sum(values1(idx))/length(idx);
Do the same for others.
댓글 수: 7
KALYAN ACHARJYA
2019년 6월 30일
편집: KALYAN ACHARJYA
2019년 6월 30일
I dont know, why you are interested to creating two cells?
Good Night!
추가 답변 (1개)
Dave
2019년 6월 30일
편집: Dave
2019년 6월 30일
Hi Max,
Since it looks like you have the time1 and values1 data in a 1 x 7 numeric array I would create a single 2 x 7 array an then transpose it. It reads better, atleast for me.
A = [time1;values1]';
Take a look at then using find() with the conditions you specified to identify those rows of A that meet the conditions. Apply this index to the 2nd column and then use the mean() function on the output.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!