How to run a function for each rows and columns?

I have a function called distance, where the values of x0 and y0 are taken from data,
data = 150*(4*rand(2,100)-1); data = data.*rand(2,100);
where, x0 is the value of first row & y0 is the value of second row, now my concern, how to run the function to each values of x0 and y0 in data and store them in list_d
below are the A, B, C parameters,
A=0.0022;B=0.0061;c=0.4229;
Thanks in advance
function list_d = distance(x0,y0,A,B,C)
P=[x0; y0 ];
Q =-C+P(2);
n1 = 2*A^2;
n2= 3*A*B;
n3=(B^2-2*A*Q+1);
n4=-B*Q-P(1);
N = [n1 n2 n3 n4];
xT=roots(N);
list_d=[]; %list of distance
d_min = Inf;
for i =1:size(xT,1)
if isreal(xT(i)) %Test each xT
T=[xT(i); A.*xT(i)^2+B.*xT(i)+C];
d =norm(P-T);
list_d = [list_d d];
if (d < d_min)
d_min=d;
end
end
end

댓글 수: 7

Jan
Jan 2018년 8월 5일
What is the problem with the shown code?
The code does not have any problem
data = 150 * (4 * rand(2,100) -1);
data = data.*rand(2,100);
which has 2*100 vectors, i have to run the code for all the values in data
The value of P in the code will be x0(the first cell of first row in data) and y0(first cell of second row in data)
dpb
dpb 2018년 8월 5일
So why can't you put another loop around this function passing the needed inputs?
Jan
Jan 2018년 8월 5일
I don't get it: What is the problem you want to solve?
I have a function called distance(attached above), In the distance(function), P is the variable which takes the value from data (ie)x0 and y0, whereas the values of A ,B and C (A = 0.0022 B = 0.0061, c = 0.4229;)are given explicitly. I tried using the cellfun but got into errors, is there any way to get it sorted?
data = 150 * (4 * rand (2,100) -1);
data = data. * rand (2,100);
dpb
dpb 2018년 8월 5일
Well, there's no cell in sight so why use it?
Again, what's the problem in just putting your function in a loop and pass the necessary vectors to it?
Anyways got the required output creating a loop Thank you

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 8월 5일

댓글:

2018년 8월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by