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 commentaires

KALYAN ACHARJYA
KALYAN ACHARJYA le 9 Sep 2019
What you have tried so far?
HT
HT le 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
Guillaume le 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
HT le 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.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 9 Sep 2019
Modifié(e) : Matt J le 10 Sep 2019

2 votes

[~,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 commentaires

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);
HT
HT le 10 Sep 2019
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.
Matt J
Matt J le 10 Sep 2019
I've updated the answer to handle the new format of B.
HT
HT le 10 Sep 2019
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.
Matt J
Matt J le 10 Sep 2019
Modifié(e) : Matt J le 10 Sep 2019
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,[]);
HT
HT le 12 Sep 2019
Thanks, I figured out and now getting the correct values.
HT
HT le 16 Sep 2019
How can I save sorted A values in a variable?
[Asorted,idx]=sort( reshape(A,8,[]) ,1);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by