2016-08-17 107 views
0

我正在学习Matlab,并且正在编写代码以根据使用'if'的条件来查找特定值。值可以很容易地找到,但我想知道这个值是由阵列A和B的哪些元素创建的。不幸的是,我不能使用这个代码。我真的很感激你的关注。使用Matlab将值赋给循环中的变量

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 
+1

,正吗? –

回答

0

只是不停的位置k和f与你为什么不创建2个变量来存储m的值指的是

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 
+0

你不需要'./'进行标量分割,而是使用'/'。 – NKN

+0

点了。我只是看着[k,f]部分 – Finn

+0

我试过了,但有一个错误:“下标索引必须是真正的正整数或逻辑。” – Naz