谁可以缩短/修改/简化Scilab代码(和JavaScript代码)?在代码中搜索多余的内容

问题描述 投票:0回答:2

Scilab 6.0.2

    function [a, ka] = srty(a, b, orien)
    // returns a values which are not in b


    // orien="r"|"c" added, including the hypermat case

    rhs = argn(2);

    // CHECKING INPUT ARGUMENTS
    // ========================
    if rhs < 2 | rhs > 3 then
        msg = gettext("%s: Wrong number of input argument(s): %d or %d expected.\n");
        error(msprintf(msg, "setdiff", 2, 3));
    end
    // Trivial case _whatever is b_
    if a==[]
        ka = []
        return
    end
    // orien
    if ~isdef("orien","l") then
        orien = 0
    elseif orien~="r" & orien~="c" & orien~=1 & orien~=2
        msg = gettext("%s: Argument #%d: Must be in the set {%s}.\n");
        error(msprintf(msg, "setdiff", 3, "''r'',''c'',1,2"));
    elseif orien=="c"
        orien = 2
    elseif orien=="r"
        orien = 1
    end
    if orien==1 & size(a,2)~=size(b,2) then
        msg = _("%s: Arguments #%d and #%d: Same numbers of columns expected.\n")
        error(msprintf(msg, "setdiff", 1, 2))
    end
    if orien==2 & size(a,1)~=size(b,1) then
        msg = _("%s: Arguments #%d and #%d: Same numbers of rows expected.\n")
        error(msprintf(msg, "setdiff", 1, 2))
    end

    // PROCESSING
    // ==========
    // "r" or "c"
    // ----------
    if orien then
        it = inttype(a)
        if ndims(a)>2 then
            a = serialize_hypermat(a, orien)
        elseif orien==2
            a = a.'
        end
        // Trivial case
        if b == [] then
            ka = 1:size(a,orien);
            if orien==1
                ka = ka'
            end
            return
        end
        if ndims(b)>2 then
            b = serialize_hypermat(b, orien)
        elseif orien==2
            b = b.'
        end
        [a, ka] = unique(a, "r")
        b = unique(b, "r")
        [c, kc] = gsort([[a iconvert(ones(a(:,1)),it)] ;
                         [b iconvert(ones(b(:,1))*2,it)]], "lr","i")
        k = find(or(c(1:$-1,1:$-1)~=c(2:$,1:$-1),"c") & c(1:$-1,$)==1)
        if c($,$)==1
            k = [k size(c,1)]
        end
        ka = ka(kc(k))
        // a = a(ka,:) // in initial order
        a = c(k,1:$-1)
        if orien==2
            ka = ka'
            a = a.'
        end
    else
        // by element
        // ----------
        [a,ka] = unique(a);
        na = size(a,"*");

        b = unique(b(:));

        [x,k] = gsort([a(:); b], "g", "i");
        d = find(x(2:$)==x(1:$-1));  //index of common entries in sorted table
        if d <> [] then
            k([d;d+1]) = [];
        end

        keep = find(k <= na);
        a = a(k(keep));
        ka = ka(k(keep));
    end
   endfunction

 function h = serialize_hypermat(h, orien)
    if orien==1 then
        dims = 1:ndims(h)
        dims([1 2]) = [2 1]
        h = permute(h, dims)
    end
    h = matrix(h, size(h,1), -1).
endfunction
a = [1,2,3,5,8];
b = [0,1,3,4,8,9,10];
k = 3; //indicated number
G = srty(a,b)
disp(G(G>k)) //Shows a number greater than the given

[我正在尝试建立一种算法来确定属于集合A \ B(差A-B)且大于集合值的元素数。

例如:A = {1,2,3,5,8},B = {0,1,3,4,8,9,10}并且设置值为3。A \ B = {2,5},但是是一个要素大于3。此值为5。

[如果有人知道该任务的另一种解决方案,我不会拒绝帮助。


我也有JavaScript代码,没有搜索大于给定数字的数字。

JavaScript

function diff_sort_arr(array_1,array_2)
{
    var n = array_1.length, m = array_2.length, i = 0, k = 0, j = 0, array_3 = [];
    while ((i < n) && (j < m)) // until we reached the end of the array 
    {
        if (array_1[i] == array_2[j])
        { 
            i++,j++;
        } else {
            if (array_1[i] < array_2[j]) {
                array_3[k] = array_1[i];
                k++;
                i++; // shift position in the first array
            } else {
                j++; // move the position in the second array
            }
        }
    }
    while (i < n) {
        array_3[k] = array_1[i];
        k++, i++;
    }
    return array_3;
} 


diff_sort_arr([1,2,3,5,8], [0,1,3,4,8,9,10]); // at the exit [2, 5]
javascript shortcode scilab
2个回答
0
投票

对于Javascript

如果array_1 [i]

跳过

function diff_sort_arr(array_1,array_2, set_value)
	{
	    var n = array_1.length, m = array_2.length, i = 0, k = 0, j = 0, array_3 = [];
	    while ((i < n) && (j < m)) // until we reached the end of the array 
	    {
	        if (array_1[i] == array_2[j] || array_1[i]<set_value)
	        { 
	            i++,j++;
	        } else {
	            if (array_1[i] < array_2[j]) {
	                array_3[k] = array_1[i];
	                k++;
	                i++; // shift position in the first array
	            } else {
	                j++; // move the position in the second array
	            }
	        }
	    }
      let ret_arr=[];
	    while (i < n) {
        array_3[k] = array_1[i];
        k++, i++;
	    }
	    return array_3;
	} 


	console.log(diff_sort_arr([1,2,3,5,8,10,12], [0,1,3,4,8,9,10], 3)); // at the exit [2, 5]

已测试工作的SciLab代码

函数[a,ka] = srty(a,b,orien)//返回不在b

中的值
    // orien="r"|"c" added, including the hypermat case

    rhs = argn(2);

    // CHECKING INPUT ARGUMENTS
    // ========================
    if rhs < 2 | rhs > 3 then
        msg = gettext("%s: Wrong number of input argument(s): %d or %d expected.\n");
        error(msprintf(msg, "setdiff", 2, 3));
    end
    // Trivial case _whatever is b_
    if a==[]
        ka = []
        return
    end
    // orien
    if ~isdef("orien","l") then
        orien = 0
   end

    // PROCESSING
    // ==========
    // "r" or "c"
    // ----------

        // by element
        // ----------
    [a,ka] = unique(a);
    na = size(a,"*");

    b = unique(b(:));

    [x,k] = gsort([a(:); b], "g", "i");
    d = find(x(2:$)==x(1:$-1));  //index of common entries in sorted table
    if d <> [] then
        k([d;d+1]) = [];
    end

    keep = find(k <= na);
    a = a(k(keep));
    ka = ka(k(keep));

   endfunction

 function h = serialize_hypermat(h, orien)
    if orien==1 then
        dims = 1:ndims(h)
        dims([1 2]) = [2 1]
        h = permute(h, dims)
    end
    h = matrix(h, size(h,1), -1)
endfunction
a = [1,2,3,5,8,10,12];
b = [0,1,3,4,8,9,10];
k = 3; //indicated number
G = srty(a,b)
H = G(G>k)
disp(H) //Shows a number greater than the given

SciLab输出

-> G = srty(a,b)G =

    1. 12。

-> H = G(G> k)H =

  1. 12。

-> disp(H)//显示大于给定的数字

  1. 12。

0
投票

如果setdiff代码(您上面粘贴的代码)无济于事,请查看以下讨论:An algorithm to find the difference of two set A and B with size n

© www.soinside.com 2019 - 2024. All rights reserved.