appending data to scatter(x,y)

조회 수: 64 (최근 30일)
cgo
cgo 2015년 10월 8일
편집: Will Reeves 2022년 3월 16일
I am n random points in two dimensional space (x,y). I am using scatter(x,y) to visualize them.
Now I want to take the center of mass of these points (x,y) with a formula, and now I want to append them to the scatter(x,y). There are now n+1 points. How do I do this, aside from using 'hold on'?
Your insights would be great

채택된 답변

Robert Dylans
Robert Dylans 2015년 10월 8일
"Center of mass" makes me hesitate a bit on this. Might want to clarify exactly what you mean by that. Do you want to connect the points in order, forming a closed shape, and calculate centroid from that? Or just use averages to find the center of the data points themselves? Or find the center of the outer bounds of the data, directly between min&max?
Assuming the second option above:
x=[0,1,1.5,3,5];
y=[5,3,1.5,1,0];
scatter(x,y);
c=[mean(x),mean(y)];
hold on;
scatter(c(1),c(2));
hold off;
This only appends the center (c) to the plot, not to the data (x,y). To do that,
x=[x c(1)];
y=[y c(2)];

추가 답변 (1개)

Will Reeves
Will Reeves 2022년 3월 16일
편집: Will Reeves 2022년 3월 16일
I would like to add scatter points to a plot during a loop to visualise the process I'm running.
I currently use hold on, and scatter the new point every time it's created, or destroyed. Unfortunately, the way Matlab handles (excuse the pun) scatter plots is fine if you are scattering a large number of points at once. If you're scattering one point at a time it eats memory like a monster if you have a few thousand individual scatter points. (I got to 20GB quickly then my PC ground to a stop!).
It would be lovely to have a feature where you can add data to a dataset or scatter plot and have it "add" the point, rather than creating a whole new object. For instance
sp=scatter(data(:,1),data(:,2));
then add a point to the data, then
sp.update
or something?
Of course this leads to another issue in Matlab whereby "adding extra points to arrays" is horrifically slow, and pre-allocation is fine if you already know the outcome. It would be lovely to have the "list functionality" that other languages have where you can efficiently add data to a list without reallocation - obviously a list object wouldn't be as fast at other types of computation though appreciated.

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by