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

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 9일
What you have tried so far?
HT
HT 2019년 9월 9일
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 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일

2 개 추천

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

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

태그

질문:

HT
2019년 9월 9일

댓글:

2019년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by