How do I exclude data coordinates from a preexisting array?

조회 수: 18 (최근 30일)
Cody Stein
Cody Stein 2015년 10월 5일
댓글: James Tursa 2015년 10월 5일
Hello!
I was wondering if anyone could help me, I am working on a project where I have a set of data inside of a matrix and I want to create a new matrix with that data using only points outside of an area defined by two functions.
For example, If I use data=rand(100,2) to create a 100X2 matrix and plot it, there is a seemingly random distribution of points. Let's say I plot y1= .5x+.45 and y2=.5x+.55. There will be some many of points inside of these two functions that I DON'T want to include in my data. How would I go about taking these out of the matrix and how would I go about plotting everything but those points?
I tried doing something like this:
>clear all
>close all
>x=linspace(-1,1,100);
>y=linspace(-1,1,100);
>limit1=.5*x+.45;
>limit2=.5*x+.55;
>data=rand(100,2);
>hold on
>plot(x,limit1);
>plot(x,limit2);
>xlim([0,1]);
ylim([0,1]);
>
>if data(:,2) < transpose(limit1)
>scatter(data(:,1),data(:,2),'r','o');
>if data(:,2) > transpose(limit2)
>scatter(data(:,1),data(:,2),'r','o');
>end
>else scatter(data(:,1),data(:,2),'*');
>end
Unfortunately it doesn't seem like it wants to plot the scatter plots correctly and I have no idea how to even start to remove the elements that don't satisfy the conditions from the array, any help would be greatly appreciated!

채택된 답변

James Tursa
James Tursa 2015년 10월 5일
편집: James Tursa 2015년 10월 5일
E.g., use logical indexing to isolate the points you want to plot.
g1 = data(:,2) < .5 * data(:,1) + .45; % Points less that lower limit
g2 = data(:,2) > .5 * data(:,1) + .55; % Points greater than upper limit
g = g1 | g2; % Union of the two point sets above
plot(data(g,1),data(g,2),'*'); % Plot only those points using g as 1st index
If you wanted to physically remove the unwanted points from your data set, then you could do this:
data = data(g,:); % Keep only the wanted points
  댓글 수: 4
Cody Stein
Cody Stein 2015년 10월 5일
So if I wanted the new matrix that included only the data points that satisfied the conditions how would I go about getting that value? Would I multiply each column of data by the logical matrix 'g'? I might be doing that incorrectly because when I do that I get what appears to be a single answer.
James Tursa
James Tursa 2015년 10월 5일
Already answered above. The following line creates a new matrix consisting only of the data points that match your conditions (i.e., keeps all x-y pairs that match the conditions):
data_new = data(g,:); % Keep only the wanted points

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

추가 답변 (0개)

카테고리

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