[英]Assigning a value to a variable in a loop using Matlab
我正在學習Matlab,並且正在編寫代碼以根據條件使用'if'查找特定值。 該值很容易找到,但是我想知道此值是由數組A和B的元素創建的。不幸的是,我無法使用此代碼。 非常感謝您的關注。
A=[2,7,1,3,10];
B=[2,7,1,3,10];
c=1;
k=0;
f=0;
L=length (A);
for m=1:L-1
for n=m:L
if(A(m)./B(n)> 0.09 && A(m)./B(n)<c)
c=A(m)./B(n);
k=A(m);
f=B(n);
end
end
end
fprintf('the c value is %0.5f',c)
fprintf('the A(m) value is %0.5f',k)
fprintf('the B(n) value is %0.5f',f)
the c value is 0.10000
只要保持位置為k和f並參考
for m=1:L-1
for n=m:L
if(A(m)./B(n)> 0.09 && A(m)./B(n)<c)
c=A(m)./B(n);
k=m; %keeping the position
f=n; %keeping the position
end
end
end
if (f==0 || k==0) %if f or k is zero
fprintf('No solution found\n')
else
fprintf('the c value is %0.5f \n',c)
fprintf('the A(m) value is %0.5f at position %i \n',A(k),k)
fprintf('the B(n) value is %0.5f at position %i \n',A(f),f)
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.