Graph analysis question

조회 수: 1 (최근 30일)
Matt
Matt 2011년 8월 3일
I have this data:
X = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
With
Y = 0.5 1 1.5 2 2.5 3 3.5 4 1 1.5 3 3.5 4.5 5.5 6 2 4 4.5 5.5 6.5 8
This may be more of a math question rather than a matlab question. If you plot the above data you will see three distinct sets of data, is there any way I can get matlab to automatically split this data into the three seperate variables. The number of variables may change depending on the data set, the data I have provided is indicative for the real data the X axis is actually dates.
In short I need Matlab to detect how many separate data sets there are and split the data into different variables.
I've tried using a max and min point script but I didn't get very far with it.
Thanks in advance
  댓글 수: 3
the cyclist
the cyclist 2011년 8월 3일
Robert, if you do plot(X,Y,'.'), you will understand better what he means. The data lie in three well defined linear groups.
Robert Cumming
Robert Cumming 2011년 8월 3일
yes I see now that I actually plotted it... Misinterpreted the question

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

채택된 답변

the cyclist
the cyclist 2011년 8월 3일
After I posted my answer about cluster analysis, I noticed the following:
Are your (X,Y) data always sorted as in your example, or can the pairs be jumbled? If they are always sorted, you can just look for negative jumps in Y, using the find() and diff() commands, and separate the data wherever there is such a jump downward.
X = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21];
Y = [0.5 1 1.5 2 2.5 3 3.5 4 1 1.5 3 3.5 4.5 5.5 6 2 4 4.5 5.5 6.5 8];
N = numel(Y);
negativeJumpIndex = find(diff([0,Y])<0);
numberNegativeJumps = numel(negativeJumpIndex);
numberClusters = numberNegativeJumps + 1;
indexToNewLine = [1 negativeJumpIndex N+1];
IDX = zeros(N,1);
for nc=1:numberClusters
IDX(indexToNewLine(nc):indexToNewLine(nc+1)-1) = nc;
end
figure
gscatter(X,Y,IDX)
  댓글 수: 1
Matt
Matt 2011년 8월 3일
Many thanks for all of the responses, the data is always sorted so I will try to look for negative jumps.
Once again, many thanks

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

추가 답변 (2개)

the cyclist
the cyclist 2011년 8월 3일
What you are asking about is generically known as "cluster analysis", and MATLAB does have some functionality for it, at least in the Statistics Toolbox. If you have that toolbox, look at "doc kmeans" as a possible starting point.
  댓글 수: 1
the cyclist
the cyclist 2011년 8월 3일
I put this answer up before I plotted out your data. Not sure that kmeans() is going to help you, because I think it is always searching for a "nearest neighbors" sort of clustering, rather than the linear groups you have. It seems to me that you have a fairly specialized problem here, and you'll likely need to write custom code for it.

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


Wolfgang Schwanghart
Wolfgang Schwanghart 2011년 8월 3일
I just tried your example. While results of a kmeans clustering don't look too promising, the function clusterdata works quite well. What you should know a priori is the number of clusters in your data.
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]';
y = [0.5 1 1.5 2 2.5 3 3.5 4 1 1.5 3 3.5 4.5 5.5 6 2 4 4.5 5.5 6.5 8]';
IDX = clusterdata([x y],'distance','chebychev','maxclust',3);
gscatter(x,y,IDX)
Regards, W.
  댓글 수: 2
the cyclist
the cyclist 2011년 8월 3일
kmeans() seems to do better than clusterdata() to me, although neither very acceptably well. kmeans() at least gets most of the points attributed to the correct lines. clusterdata() combines almost all of the middle and right-hand lines into one cluster (at least with the syntax you provided).
Matt
Matt 2011년 8월 3일
Thank you for the help

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

Community Treasure Hunt

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

Start Hunting!

Translated by