how to program this function

1 view (last 30 days)
t sizzle
t sizzle on 21 Apr 2021
Commented: Rena Berman on 6 May 2021
I am trying to program a function (as a separate matlab function, not in the main script) which takes values for variables from different arrays and uses them in the calculation. Here is the function
I am running into a problem because I need to test every A and B value for each coordinate ([1,8], [3,9], [4,2], [9,4]), but since the arrays are different sizes it keeps returning errors.
None of the methods I have tried has worked. The different example arrays I have are as follows.
a = [2:0.2:20];
b = [2:0.2:20];
x = [1,3,4,9];
y = [8,9,2,4];
%numerical values will change based on parameters the user enters. Will
%always be the same size though.
  2 Comments
Stephen23
Stephen23 on 28 Apr 2021
Edited: Stephen23 on 28 Apr 2021
Original question retrieved from Google Cache:
how to program this function
I am trying to program a function (as a separate matlab function, not in the main script) which takes values for variables from different arrays and uses them in the calculation. Here is the function
I am running into a problem because I need to test every A and B value for each coordinate ([1,8], [3,9], [4,2], [9,4]), but since the arrays are different sizes it keeps returning errors.
None of the methods I have tried has worked. The different example arrays I have are as follows.
a = [2:0.2:20];
b = [2:0.2:20];
x = [1,3,4,9];
y = [8,9,2,4];
%numerical values will change based on parameters the user enters. Will
%always be the same size though.
Rena Berman
Rena Berman on 6 May 2021
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

DGM
DGM on 21 Apr 2021
Reorient the vectors. If you're using R2016b or newer, you can do this:
c = sum(sqrt((a-x').^2 + (b-y').^2),1)
otherwise, you can use bsxfun(). The point is to generate a distance vector for each x,y pair, resulting in a 2D array. Then just sum the rows.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!