Function handling problem in loop
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!