Making my function run for multiple input values

조회 수: 8 (최근 30일)
Eric
Eric 2019년 11월 14일
댓글: Walter Roberson 2019년 11월 14일
function t = TrueThickness(strike,dip,pTop,pBase)
% transformatiion matrix
a = [sin(strike) cos(strike) 0;...
cos(strike)*cos(dip) -sin(strike)*cos(dip) -sin(dip);...
-cos(strike)*sin(dip) sin(strike)*sin(dip) -cos(dip)];
% transform points
p3n = zeros(3);
p4n = zeros(3);
for i = 1:3
for j = 1:3
p3n(i) = a(i,j)*pTop(j) + p3n(i);
p4n(i) = a(i,j)*pBase(j) + p4n(i);
end
end
% compute thicknessess
tt = abs(p3n(3) - p4n(3))
end
saved file as test12414!!
Here I make new matlab file and try running my function
-----------------------------------------------------------------------------------------------------------------------------------
pTop= [724277 4944457 1270]
pBase = [ 724371 4944506 1299]
strike = 161.5651
dip = 16.0389
test12414(strike,dip,pTop,pBase)
the code works and is great but I want to be able to have several values for my strike,
dip ,pTop and pBase. for example lets say i want to use the script 3 times for different values.
Then pTop and pBase will be 3x3 vectors so have 9 numbers and strike and dip will have 3 values
how would I do this?

답변 (1개)

ME
ME 2019년 11월 14일
편집: ME 2019년 11월 14일
You could call the function inside a ‘for’ loop. That would enable you to feed in the different input values. For example:
In1=[val1 val2 val3];
In2=[val1 val2 val3];
for i=1:3
[out1(i) out2(i)]=function(In1(i) in2(i))
end
Obviously, adjust for your particular application.
  댓글 수: 2
Eric
Eric 2019년 11월 14일
편집: Eric 2019년 11월 14일
Thank you for you answer this makes sense but I ran into a small problem (I think).
test12414(strike,dip,pTop,pBase) I replaced this with:
for b=1:3
out(b,:) = test12414(strike(b), dip(b),pTop(b,:),pBase(b,:))
end
gives error:
Output argument "t" (and maybe others) not assigned during call to "test12414".
Error in svarararada (line 12)
out(b,:) = test12414(strike(b), dip(b),pTop(b,:),pBase(b,:))
any idea how to fix this?
And my function is supposed to output 1 value but since I have b=1:3 it should give 3 values
Walter Roberson
Walter Roberson 2019년 11월 14일
You define
function t = TrueThickness(strike,dip,pTop,pBase)
but you never assign anything to t . You do, however, assign to tt but do not use tt after the assignment.

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

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by