How do I exclude data coordinates from a preexisting array?
24 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
James Tursa
on 5 Oct 2015
Edited: James Tursa
on 5 Oct 2015
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 Comments
James Tursa
on 5 Oct 2015
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
More Answers (0)
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!