MATLAB Answers

Sorting and averaging matrices

1 view (last 30 days)
HT on 9 Sep 2019
Commented: Matt J on 16 Sep 2019
  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!


Show 1 older comment
HT on 9 Sep 2019
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.
Guillaume on 9 Sep 2019
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 on 9 Sep 2019
It will be a multiple of 8 always. I just took 100 as a random number. I will edit the main question, thank you.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 9 Sep 2019
Edited: Matt J on 10 Sep 2019
[~,idx]=sort( reshape(A,8,[]) ,1);
result=reshape( C ,m*n/5,[]);


Show 5 older comments
HT on 12 Sep 2019
Thanks, I figured out and now getting the correct values.
HT on 16 Sep 2019
How can I save sorted A values in a variable?
Matt J
Matt J on 16 Sep 2019
[Asorted,idx]=sort( reshape(A,8,[]) ,1);

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!