What about other elements (i.e. 1, 2, 4 and 5) in A. What is your expected output for the given A and B. What have you tried so far?
How to find the indices of same values within a vector and bin them together
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
I have two vectors with the same length (N). For example:
A=[ 1 2 3 3 3 3 4 5]; B=[ 9 8 7 6 5 6 7 8];
What I need to do is to take an average over elements in vector 'B' that their corresponding indices in vector 'A' have same values. In this example, element 3,4,5,6 in vector A are all equal to '3' so I need to take an average over (7+6+5+6/4) in vector B. I need to do this over a large data and bin them together. I tried different ways and it didn't work. How can I do this?
댓글 수: 3
채택된 답변
  Ameer Hamza
      
      
 2018년 4월 24일
        The following statement will return the list of all the averages according to the way you mentioned in your question.
meanVector = splitapply(@(x) mean(x), B', A')
since A have 5 unique elements, meanVector will also contain 5 elements corresponding to the mean value from B.
댓글 수: 10
추가 답변 (1개)
  Star Strider
      
      
 2018년 4월 25일
        
      편집: Star Strider
      
      
 2018년 4월 25일
  
      See if this works with your data:
A=[ 1 2 3 3 3 3 4 5];
B=[ 9 8 7 6 5 6 7 8];
[Au,~,ix] = unique(A(:),'stable');                      % Unique Elements & Indices (Convert To Column Vector), Keep Original Order
Ar_mean = accumarray(ix, B, [], @mean);                 % Means Of Single & Repeated Values
Result = [Au, Ar_mean]                                  % Elements Of ‘A’ In Column #1, Corresponding ‘mean’ Of ‘B’ In Column #2
AB_Tbl = table(Au, Ar_mean)                             % Table (Optional)
Result =
     1     9
     2     8
     3     6
     4     7
     5     8
AB_Tbl =
    Au    Ar_mean
    __    _______
      1        9   
      2        8   
      3        6   
      4        7   
      5        8
댓글 수: 2
참고 항목
카테고리
				Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


