# A function with 2 inputs, how to make it accept a 2-by-1 array as an input?

1 visualizzazione (ultimi 30 giorni)
Xiaohan Du il 13 Nov 2017
Commentato: Xiaohan Du il 13 Nov 2017
Hi all,
There are some functions which accepts 2 inputs, such as inpolygon. For example:
K>> xv
xv =
1
2
2
1
K>> yv
yv =
1
1
2
2
K>> x = 1.5
x =
1.5000
K>> y = 1.5
y =
1.5000
K>> inpolygon(x, y, xv, yv)
ans =
logical
1
Which is completely fine. However, if I have an array which contains x-y coordinates, such as:
K>> xy = [1.5 1.5]
xy =
1.5000 1.5000
How can I use xy as an input instead of using x and y separately? I do not want to extract x and y manually like:
K>> x = xy(1), y = xy(2)
I want something like:
K>> inpolygon(xy, x, y)
Not enough input arguments.
Error in inpolygon (line 64)
if ~isvector(xv) || ~isvector(yv)
but there is error. Any ideas?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

KSSV il 13 Nov 2017
function out = myinpolygon(xy,x,y) ;
out = inpolygon(xy(:,1),xy(:,2),x,y) ;
end
:)
##### 6 CommentiMostra 4 commenti meno recentiNascondi 4 commenti meno recenti
Xiaohan Du il 13 Nov 2017
I see, we can do this:
K>> inpt = {1 2}
inpt =
1×2 cell array
[1] [2]
K>> inpolygon(inpt{:}, xv, yv)
ans =
logical
1
such that only 1 input is needed (of course there are still 2 elements in this input).
Xiaohan Du il 13 Nov 2017
Thank you Stephen!

Accedi per commentare.

### Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by