# How do I plot a series of points as a collection of 1x2 arrays?

3 views (last 30 days)
Ryan Begley on 24 Mar 2020
Commented: Sindar on 24 Mar 2020
I need to write a function that will plot a series of points that are input as 1x2 arrays separated by commas. I understand that one could simply input all of the x values into one array and all of the y values in to an array (ie. x = [1 4 3 7 2] y = [2 5 1 2 8] plot(x,y,'*'),) however for this problem i am required to input the points as 1x2 arrays separated by collumns like so:
function plotPoints(points);
%insert code here;
end
then in a new script:
plotPoints([1,2] , [2,6] , [8,3] , [6,9])
I have no idea how to go about doing this.

Sriram Tadavarty on 24 Mar 2020
Ryan,
I am not sure how do you pass such a sequence as inputs? Is it through cell array or a array of size N x 2?
Ryan Begley on 24 Mar 2020
Sriram,
I am not sure either, all I know is that I have to write a function that will accept an arbitrary number of points as 1x2 arrays [x,y] and plot them. And then i am to demonstrate the use of the function by running
plotPoints([1,2] , [2,6] , [8,3] , [6,9])
I just have no idea how to get it to plot all of the points
Sindar on 24 Mar 2020
You could use varargin:
function plotPoints(varargin);
%insert code here;
end
which will produce a cell array as I describe in my answer (in the variable varargin instead of points)

Sindar on 24 Mar 2020
If you have a cell array of 1x2 arrays like so:
points = {[1,2] , [2,6] , [8,3] , [6,9]};
then you can turn it into a 1x (2N) array, then reshape it to a 2xN array:
points = reshape(cell2mat(points),2,[]);
from there, use Sriram's answer (except the first row is x):
plot(points(1,:),points(2,:),'*')
If you start with a 1x(2N) array, you can remove the cell2mat() call:
points = reshape(points,2,[]);