Rearranging rows using unique values in column

1 次查看(过去 30 天)
If I have a matrix as follows: The first column is unique ID while the second column represents price:
x = [106 2;
106 4;
109 3;
109 7;
109 11;
106 3;
106 6;
109 16]
I want to arrange the matrix using the unique values in the first column to get the following:
x = [106 2;
106 4;
106 3;
106 6;
109 3;
109 7;
109 11;
109 16]
After arranging the matrix I want to calculate the change in prices for each unique ID to get the following
newdatawithchange = [106 2 --;
106 4 2;
106 3 -1;
106 6 3;
109 3 --;
109 7 4;
109 11 4;
109 16 5]
Then, how can I calculate the correlation between values in the second and third column but unique for each ID (There will be a coefficient for ID 106 and another coefficient for ID 109 with skipping the first row for each ID as there is no value for the change)

采纳的回答

Image Analyst
Image Analyst 2017-5-5
To sort it is easy:
x = [106 2;
106 4;
109 3;
109 7;
109 11;
106 3;
106 6;
109 16]
sortedX = sortrows(x, 1)
  7 个评论
Image Analyst
Image Analyst 2017-5-5
Is this homework? I think you would learn more if you tried something yourself than just asking me to do it all for you. For example, did you look up corr in the help? Did you see this:
RHO = corr(X,Y) returns a p1-by-p2 matrix containing the pairwise correlation coefficient between each pair of columns in the n-by-p1 and n-by-p2 matrices X and Y.
The difference between corr(X,Y) and the MATLAB® function corrcoef(X,Y) is that corrcoef(X,Y) returns a matrix of correlation coefficients for the two column vectors X and Y. If X and Y are not column vectors, corrcoef(X,Y) converts them to column vectors.
I don't think this is beyond your capabilities, is it? Just pass in the second and third columns, except for the rows I identified as startRows. X is col2 and Y is col3. Give it a try.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by