Function handling problem in loop

1 view (last 30 days)
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

Accepted Answer

Alan Stevens
Alan Stevens on 13 Jan 2021
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 Comments
Stephen23
Stephen23 on 13 Jan 2021
"...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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!