繁体   English   中英

如何在不使用最小值或特制功能的情况下找到最小值

[英]How to find a minimum value without using min or specifically made functions

function [c] = nextinteger(v)
c=0;
a =0;
h=[0];
for i= 1:length(v)
    if v(i)>0
        h=v;
        if h(i+1)>h(i)
            a = h(i);
            c=a+1;
        end 
    end
end
end

我必须“编写一个函数nextinteger(v) ,该函数将向量v作为输入,而输出则返回不出现在v的最小正整数。”

目前,我似乎找不到解决方案,而上面的代码是我所能找到的解决方案。 有人可以告诉我我需要考虑什么以及需要解决什么才能更接近我的解决方案

预期结果示例:如果输入为

[1, 2, 3]

,输出将是

4

如果输入是

[4, 0, 1, -10], 

输出将是

2

输入示例

v=[10 0 1 -1 3 4 5];

n=length(v); % length of input
tmp=1:n; % possible integer numbers
for k=1:n % loop over vector
    comp_value=v(k);

    for r=1:length(tmp) %loop over reference
        if tmp(r)==comp_value %compare 
           tmp(r)=[]; %eclude
           break
    end
    end
end

if isempty(tmp) % check whether all numbers are gone
    result=n+1;
else
    result=tmp(1);
end

输出:

result =

 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM