필터 지우기
필터 지우기

Calculating Average Duration/Length in an Array

조회 수: 1 (최근 30일)
David
David 2014년 12월 12일
댓글: Star Strider 2014년 12월 12일
Hello,
I have an array that contains 1 2 3 which each represents a state i.e) 1=sunny day 2=rainy day 3=cloudy day. The array is about 1 million length.
Example: [3 3 3 1 1 1 1 2 1 1 2 3 3]
I would like to figure out the average duration of each state. For state 1, it would be (4+2/2)= 3 days. For state 2, it would be (1+1/2=)1day. And state 3 it would be (3+2/2)=2.5days
I'm wondering if anyone has any suggestion or tips on how to do this with minimal code.
Cheers, David

채택된 답변

Star Strider
Star Strider 2014년 12월 12일
The histc function (with help from unique) is probably the easiest option:
W = [3 3 3 1 1 1 1 2 1 1 2 3 3];
bins = unique(W);
k = histc(W,bins);
avg = k./2;
  댓글 수: 2
David
David 2014년 12월 12일
Thanks, I got it all figured out now. Your advice and http://www.mathworks.com/matlabcentral/newsreader/view_thread/331664 helped me do it
Star Strider
Star Strider 2014년 12월 12일
My pleasure!
I thought you might want to find out the number of repeated sequences next, but I opted to wait and find out. I’m glad you found an applicable solution to the repeated-sequence problem.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Markov Chain Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by