MATLAB:循环内的 If 语句未执行,也未打印到屏幕

发布于 2024-10-20 00:35:24 字数 944 浏览 4 评论 0原文

因此,我们尝试执行以下代码。两个 if 语句正在执行,但是内部的 if 语句无法执行(我们通过不抑制输出来验证这一点)。有理由吗?或者我们只是无法达到这种状态?

规格

输入如下:v是int值的向量,c是整数。 c 必须小于或等于 v 中的值之一

我们试图用该算法解决的问题如下:

给定一个收银机,如何找零才能用最少的硬币 可能会退回给客户吗?

例如:输入:v = [1, 10, 25, 50],c = 40。输出 O = [5, 1, 1, 0]

我们只是在寻找不是更好的解决方案,而是更多地寻找该部分的原因该代码没有执行。

function O = changeGreedy(v,c)
  O = zeros(size(v,1), size(v,2));
  for v_item = 1:size(v,2)
         %locate largest term
         l_v_item = 1
         for temp = 2:size(v,2)
            if v(l_v_item) < v(temp)
                l_v_item = temp
            end    
         end    

    %"Items inside if statement are not executing"
    if (c > v(l_v_item))
        v(l_v_item) = -1 %"Not executing"
    else
        O(l_v_item) = idivide(c, v(l_v_item)) %"Not executing"
        c = mod(c, v(l_v_item)) %"Not executing"
    end      
end

So, we are trying to execute the following code. The two if statements are executing, however, the inside if statements are failing to execute (we verified this by not suppressing the output). Is there a reason why? Or are we just not able to reach this state?

Specifications

The input is as follows: v is a vector of int values and c is a integer. c must be less than or equal to one of the values within v

The problem that we are trying to solve with this algorithm is as follows:

Given a cash register, how does one make change such that the fewest coins
possible are returned to the customer?

Ex: Input: v = [1, 10, 25, 50], c = 40. Output O = [5, 1, 1, 0]

We are just looking for not a better solution but more of a reason why that portion of the code is not executing.

function O = changeGreedy(v,c)
  O = zeros(size(v,1), size(v,2));
  for v_item = 1:size(v,2)
         %locate largest term
         l_v_item = 1
         for temp = 2:size(v,2)
            if v(l_v_item) < v(temp)
                l_v_item = temp
            end    
         end    

    %"Items inside if statement are not executing"
    if (c > v(l_v_item))
        v(l_v_item) = -1 %"Not executing"
    else
        O(l_v_item) = idivide(c, v(l_v_item)) %"Not executing"
        c = mod(c, v(l_v_item)) %"Not executing"
    end      
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-10-27 00:35:24

如果 cv 不是整数,即 class(c) 计算结果为 double,那么我得到以下结果错误消息

??? Error using ==> idivide>idivide_check at 66
At least one argument must belong to an integer class.

Error in ==> idivide at 42
idivide_check(a,b);

并且程序停止执行。因此,第二条语句的内部永远不会执行。相反,如果 c 是一个整数,例如 uint8 类,则一切都会正常执行。

另外:您实际上想用这段代码实现什么目的?

If c or v are not integers, i.e. class(c) evaluates to double, then I get the following error message

??? Error using ==> idivide>idivide_check at 66
At least one argument must belong to an integer class.

Error in ==> idivide at 42
idivide_check(a,b);

and the program stops executing. Thus, the inside of the second statement never executes. In contrast, if, say, c is an integer, for example of class uint8, everything executes just fine.

Also: what are you actually trying to achieve with this code?

嘴硬脾气大 2024-10-27 00:35:24

尝试对您的输入数据执行此操作:

v = int32([1, 10, 25, 50]), c = int32(40)

然后再次运行,至少代码的某些部分将执行。 idivide 引发了一个错误,显然您错过了:

??? Error using ==> idivide>idivide_check at 67
At least one argument must belong to an integer class.

Error in ==> idivide at 42
idivide_check(a,b);

确实,idivide 似乎要求您有实际整数输入数据(即、class(c)class(v) 均计算为整数类型,例如 int32)。

Try to do this operation on your input data:

v = int32([1, 10, 25, 50]), c = int32(40)

and run again, at least some portions of your code will execute. There is an error raised by idivide, which apparently you missed:

??? Error using ==> idivide>idivide_check at 67
At least one argument must belong to an integer class.

Error in ==> idivide at 42
idivide_check(a,b);

Indeed, idivide seems to require that you have actual integer input data (that is, class(c) and class(v) both evaluate to an integer type, such as int32).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文