Matlab的算法问题 [英] Matlab algorithm issue

查看:192
本文介绍了Matlab的算法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,球员,我有一个问题,开发一个算法,需要被整理以递增的顺序与实数N,实数的有限列表和

So guys, I have a problem developing an algorithm that takes a finite list of real numbers that are sorted in increasing order and a real number N, and

- 如果存在两个指数Ĵ,使得 1< = I< J< = numel(L) L(我)+ L(J)= N ,然后再算法返回对 sumToN = [L(我)l(J)] ;在多个对指数用所需属性存在的情况下,该函数返回有效对之一,

-If there exists two indices i and j, such that 1 <= i <j <= numel(L) and L(i)+L(j)=N, then then algorithm returns the pair sumToN = [L(i) L(j)]; in the case that multiple pairs of indices exist with the required property, the function returns one of the valid pairs,

- 如果平等 L(我)+ L(J)= N 不符合任何指数Ĵ,使得 1&LT; = I&LT; J&LT; = numel(L),该函数返回空对惩戒

-If the equality L(i)+L(j)=N is not satisfied for any indices i and j, such that 1 <= i < j <= numel(L), the function return the empty pair Ret.

L = [1 2 2 3];
N = 3;
sumToN = 0;

for i=1:numel(L);
  for j=1:numel(L);
    if i<j;
        if L(i) + L(j) == N;

        sumToN = [L(i) L(j)];
    display(sumToN);
        else
        Ret = [0 0];
        display(Ret);
        end
    end
   end
end

现在,在我写在Matlab R2014独立的,如果条件下,我得到一个奇怪的输出该code:命令窗口中显示两次矢量 sumToN 和四倍的矢量惩戒。如何解决这个问题的任何建议?我认为算法是正确的......

Now, in this code that I've written in Matlab R2014, independently of the if conditions, I get a strange output: the command window displays two times the vector sumToN and four times the vector Ret. Any suggestion about how to solve this problem? I think that the algorithm is correct...

推荐答案

让我们解构你的程序了一下。我会做的第一件事就是从 I,J A改变循环变量,B ,因为这个:<一href="http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab/14790765#14790765">Using i和j作为在Matlab 的变量

Let's deconstruct your program a bit. The first thing I'm going to do is change the loop variables from i, j to a, b, because of this: Using i and j as variables in Matlab.

接下来,我将看看行

if i<j;

我们总是希望这是事实,所以我们让这种方式。 (切换到 A,B 现在)...

We always want this to be the case, so let's make it that way. (Switching to a, b now)...

for a=1:numel(L)-1
   for b=a+1:numel(L)

请注意,这家B 环路现在开始在值 A + 1 。因此, B 总是的比更大,我们不再需要此检查。我也结束了 numel一个循环(L)-1 因为没有元素后 L(numel(L)) B 来占领,我们要确保 A + 1 始终有效。
(另请注意,我已删除了分号线的两端,他们是不必要的,有效地添加一个空行的循环。)

Notice that the loop for b now begins at the value a+1. So b is always greater than a and we no longer need this check. I've also ended the a loop at numel(L)-1 because there is no element after L(numel(L)) for b to occupy and we want to make sure that a+1 is always valid.
(Also note that I've removed the semicolon at the ends of the lines. They're unnecessary and effectively add a blank line to the loop.)

现在我们已经准备好,看 L(一),L(B)对增加 N 如果是的话,设置退货值:

Now we're ready to see if the L(a), L(b) pair adds to N and if so, set our return value:

if L(a)+L(b) == N
   sumToN = [L(a) L(b)];   

如果你想在这里显示的数值,可以做的:

If you want the value displayed here, either do:

   display(sumToN);

或离开分号关分配的 sumToN = ...

如果我们的如果里面,我们已经分配的返回值,我们要停止寻找答案。我将承担在这一点上,你应该可以写一个独立的功能,这是所有的功能应该做的事。要结束循环(事实上,该功能的任何进一步的处理),需要使用返回转让之后。这使得嵌套的循环是这样的:

If we're inside the if and we've assigned the return value, we want to stop looking for other answers. I'm going to assume at this point that you're supposed to be writing a standalone function and that this is all the function is supposed to do. To end the loops (and, indeed, any further processing of the function) you would use return immediately after the assignment. That makes the nested loops look like this:

sumToN = [0 0];
for a=1:numel(L)-1
   for b=a:numel(L)
      if L(a)+L(b) == N
         sumToN = [L(a) L(b)];
         return
      end
   end
end

在这里,您可以在开始和<$ C $添加初始化为 N C> DISP(sumToN); 在最后一个脚本,或者你可以做一个全面的功能与函数的声明和另一个结束

From here, you can either add your initializations for L and N at the beginning and disp(sumToN); at the end for a script, or you can make a full-fledged function with a function declaration and another end:

function sumToEnd = summy(L,N)
   <all the stuff above goes here>
end

事情是,如果这是你不希望完全退出一旦你找到一对较大的脚本的一部分稍微复杂一点,但它仍然是完全可能的。 (你将不得不使用破发两次,这离开了单圈每一次,搭配一个布尔变量来的时候,打破告诉你。)

Things are a little more complicated if this is part of a larger script that you don't want to completely exit once you find a pair, but it's still quite possible. (You would have to use break twice, which exits out of a single loop each time, paired with a boolean variable to tell you when to break.)

这篇关于Matlab的算法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆