Function handling problem in loop

조회 수: 2 (최근 30일)
Amin Waqas
Amin Waqas 2021년 1월 13일
댓글: Amin Waqas 2021년 1월 15일
Dear Researcher.
Might be it will be a silly question for you but i am stuck at this point . I have a two function which calulates minimum and max values as given below:
function out=fns1(X)
x1=X(:,1);
x2=X(:,2);
x3=X(:,3);
x4=X(:,4);
x5=X(:,5);
x6=X(:,6);
x7=X(:,7);
x8=X(:,8);
x9=X(:,9);
x10=X(:,10);
out = 5.16.*x1+5.12.*x2+4.89.*x3+4.79.*x4+4.99.*x5+5.45.*x6+4.65.*x7+4.99.*x8+5.45.*x9+5.5.*x10+3;
and the other is :
function out= fns(X)
x1=X(:,1);
x2=X(:,2);
x3=X(:,3);
x4=X(:,4);
x5=X(:,5);
x6=X(:,6);
x7=X(:,7);
x8=X(:,8);
x9=X(:,9);
x10=X(:,10);
fx =5.16.*x1+5.12.*x2+4.89.*x3+4.79.*x4+4.99.*x5+5.45.*x6+4.65.*x7+4.99.*x8+5.45.*x9+5.5.*x10+3;
for i=1:size(fx,1)
if fx(i,:)>=0
fit(i,:)=1./(1+fx(i,:));
elseif fx(i,:)<0
fit(i,:)=1+abs(fx(i,:));
end
end
out=fit;
I want to use function with different values of x1 x2 to x10. These values have in text form i want to know how i can get these values and use them in function for each iteraion just like that
for i=1:100
function out=fns1(X)
x1(i)=X(:,1);
x2(i)=X(:,2);
x3(i)=X(:,3);
x4(i)=X(:,4);
x5(i)=X(:,5);
x6(i)=X(:,6);
x7(i)=X(:,7);
x8(i)=X(:,8);
x9(i)=X(:,9);
x10(i)=X(:,10);
out (i)= 5.16.*x1+5.12.*x2+4.89.*x3+4.79.*x4+4.99.*x5+5.45.*x6+4.65.*x7+4.99.*x8+5.45.*x9+5.5.*x10+3;
help me out plz.
Thans in advance

채택된 답변

Alan Stevens
Alan Stevens 2021년 1월 13일
Try
doc str2num
to convert from text to numeric.
Also, you might find it easier to define:
k = [5.16 5.12 4.89 4.79 4.99 5.45 4.65 4.99 5.45 5.5]';
and then calculate
fx = X*k+3;
etc.
  댓글 수: 3
Stephen23
Stephen23 2021년 1월 13일
"...these values also changes with respect to loop actually i want to use a look for value 1:100 so that at each iteration x1 to x10 values changes with respect to loop index"
Put all of the values into a 100x10 matrix. Inside the loop use indexing to access each row.
Don't make this more complex than it needs to be.
Amin Waqas
Amin Waqas 2021년 1월 15일
Thanks Stephen

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by