Sorting and averaging matrices

조회 수: 2 (최근 30일)
HT
HT 2019년 9월 9일
댓글: Matt J 2019년 9월 16일
  1. I have two matrices. One for example is A = 520 x 1 and it has numbers from 1-8 which are not in order. For every number in this matrix, I have corresponding 1000 samples in another matrix B 520 x 1000. I want to first sort A and arrange numbers from 1-8 for every 8 rows. So output should be (1,2,3,4,5,6,7,8,1,2,3,4,5 and so on) and arrange corresponding values in B also such that they match.
  2. Once this is done, I want to average every 5 rows of B and store it in a new matrix which will be 104 x 1000.
Thanks for the help in advance!
  댓글 수: 4
Guillaume
Guillaume 2019년 9월 9일
100 is not a multiple of 8. Isn't that a problem? Why average every 5 rows when you have groups of size 8?
HT
HT 2019년 9월 9일
It will be a multiple of 8 always. I just took 100 as a random number. I will edit the main question, thank you.

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

채택된 답변

Matt J
Matt J 2019년 9월 9일
편집: Matt J 2019년 9월 10일
[~,idx]=sort( reshape(A,8,[]) ,1);
[m,n]=size(idx);
C=reshape(B,m,n,[]);
[m,n,p]=size(C);
idx=idx+(0:n-1)*m+reshape(0:p-1,1,1,p)*(m*n);
C=mean(reshape(C(idx),5,[]));
result=reshape( C ,m*n/5,[]);
  댓글 수: 8
HT
HT 2019년 9월 16일
How can I save sorted A values in a variable?
Matt J
Matt J 2019년 9월 16일
[Asorted,idx]=sort( reshape(A,8,[]) ,1);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by