向量作为函数在matlab中的输入?

时间:2022-06-09 21:21:54

i'd like to Write a function that receives a 2x1 matrix cointaining the x and y.(no scalar inputs) is it possible? I tried as below:

我想写一个函数它接收到一个2x1矩阵并将x和y结合起来。(没有标量输入)可能吗?我尝试了如下:

function [d] = dist(A,B)

d=sqrt(((A(1)-B(1))^2+(A(2)-B(2))^2));
end

A and B are 2*1 matrices . how to put vector as function's input??

A和B是2*1矩阵。如何将向量作为函数的输入?

2 个解决方案

#1


1  

If you want to pass two vectors (since you have A and B each having two elements) as a single parameter, you can either create a 2x2 matrix or a 4x1 vector to pass in. Or you can decide to pass in a cell array (which gives you a bit more flexibility). Example:

如果你想传递两个向量(因为你有A和B两个元素)作为一个参数,你可以创建一个2x2矩阵或者一个4x1向量。或者,您可以决定传入一个单元格数组(这将使您具有更大的灵活性)。例子:

A = [1 2];
B = [4 5];

C = [A; B];

d = myDistance(C);

function m = myDistance(x)
  dxy = diff(x); % do both x(2,1) - x(1,1) and x(2,2) - x(1,2) in one operation
  m = sqrt(sum(dxy.^2));

Alternatively, passing A and B as two separate parameters (which makes a lot of sense from readability) should work in the way you described in your question...

或者,将A和B作为两个单独的参数传递(这对可读性有很大的帮助)应该按照您在问题中描述的方式工作……

#2


1  

I have a different interpretation of your question, I think you're asking how to make your function output all the distances for two lists of (x,y) coords? If so then like this:

我对你的问题有不同的解释,我想你问的是如何让函数输出两个(x,y)余量的列表的所有距离?如果是这样的话:

function [d] = Dist(A,B)
    d=sqrt(((A(:,1)-B(:,1)).^2+(A(:,2)-B(:,2)).^2));
end

So you need to change your ^ from a matrix operation to an elementwise vector operation .^ and then you need to access the first column but ALL the rows i.e. (1,:)

所以你需要改变你的^从elementwise向量的矩阵运算操作。^然后你需要访问第一列,但所有的行即(1:)

e.g.

如。

A = [0,0;
     0,1;
     1,1]
B = [1,1;
     1,1;
     1,1]

Dist(A,B)

ans =

   1.41421
   1.00000
   0.00000

By the way you could neaten up this function like this and still get the same result:

顺便说一下,你可以把这个函数整理一下,仍然得到相同的结果:

function [d] = Dist(A,B)
    d=sqrt(sum((A-B).^2,2));
end

#1


1  

If you want to pass two vectors (since you have A and B each having two elements) as a single parameter, you can either create a 2x2 matrix or a 4x1 vector to pass in. Or you can decide to pass in a cell array (which gives you a bit more flexibility). Example:

如果你想传递两个向量(因为你有A和B两个元素)作为一个参数,你可以创建一个2x2矩阵或者一个4x1向量。或者,您可以决定传入一个单元格数组(这将使您具有更大的灵活性)。例子:

A = [1 2];
B = [4 5];

C = [A; B];

d = myDistance(C);

function m = myDistance(x)
  dxy = diff(x); % do both x(2,1) - x(1,1) and x(2,2) - x(1,2) in one operation
  m = sqrt(sum(dxy.^2));

Alternatively, passing A and B as two separate parameters (which makes a lot of sense from readability) should work in the way you described in your question...

或者,将A和B作为两个单独的参数传递(这对可读性有很大的帮助)应该按照您在问题中描述的方式工作……

#2


1  

I have a different interpretation of your question, I think you're asking how to make your function output all the distances for two lists of (x,y) coords? If so then like this:

我对你的问题有不同的解释,我想你问的是如何让函数输出两个(x,y)余量的列表的所有距离?如果是这样的话:

function [d] = Dist(A,B)
    d=sqrt(((A(:,1)-B(:,1)).^2+(A(:,2)-B(:,2)).^2));
end

So you need to change your ^ from a matrix operation to an elementwise vector operation .^ and then you need to access the first column but ALL the rows i.e. (1,:)

所以你需要改变你的^从elementwise向量的矩阵运算操作。^然后你需要访问第一列,但所有的行即(1:)

e.g.

如。

A = [0,0;
     0,1;
     1,1]
B = [1,1;
     1,1;
     1,1]

Dist(A,B)

ans =

   1.41421
   1.00000
   0.00000

By the way you could neaten up this function like this and still get the same result:

顺便说一下,你可以把这个函数整理一下,仍然得到相同的结果:

function [d] = Dist(A,B)
    d=sqrt(sum((A-B).^2,2));
end