Sorting and averaging matrices

  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 个评论

What you have tried so far?
I kind of dealt with the point 1. So i concatenated A and B and sorted rows according to numbers from 1-8. I am looking to see if there is a better method to do this and point 2 which is averaging every 5 rows.
100 is not a multiple of 8. Isn't that a problem? Why average every 5 rows when you have groups of size 8?
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 个评论

This can be done without a loop:
[~, order] = sort(reshape(A, 8, []), 1);
C = mean(reshape(B, 5, []), 1);
ngroups = size(order, 2);
nsamples = size(B, 2);
result = reshape(C(order + (0:ngroups-1)*8 + permute((0:nsamples-1)*8*ngroups, [1, 3, 2])), [], nsamples);
Thank you @Matt J and @Guillaume for your inuputs.
I tried sorting using @Matt J's method but it did not sort the elements.
Also from the question, A and B actually have same number of rows so A will be 520 x 1. After sorting A & B there should be still 520 rows and then after taking mean result should be 104 x 1000. @Guillaume your method gives the result as 520 x 1000.
I've updated the answer to handle the new format of B.
Thanks @Matt J. This seems to work and I am getting the correct size of the result matrix but the mean values of result are different from those I calculated manually for a few rows. Is this method calculating mean for rows 1-5,6-10,11-15 and so on? if you can comment the steps that might be helpful to debug.
Here is another way of writing the code that breaks it into two parts. In the first part, we compute the sorted version of B.
[~,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);
Bsorted=reshape(C(idx),size(B));
and then we compute the mean of very 5 elements of Bsorted, giving the final result
result= mean( reshape(Bsorted,5,[]) ,1);
result=reshape(result,m*n/5,[]);
Thanks, I figured out and now getting the correct values.
How can I save sorted A values in a variable?
[Asorted,idx]=sort( reshape(A,8,[]) ,1);

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File 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