excel 在带有变量的矩阵中调用值时,Scilab“无效索引”

yxyvkwin  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(124)

不知何故,我是Scilab的新手,但我有一个问题,我不能弄清楚为什么会发生这种情况。
我正在将原来在Excel中的代码“转换”到Scilab中,但是当我创建类似xlookup函数的东西时,
下面是部分代码

//Import Power Curve from Excel
function [K,Kx,Ky] = ImportExcel(sheetname,sheetnumber);
    mode(0)
    sheets = readxls(sheetname);
    s1 = sheets(sheetnumber);
    s1 = s1.value;     //Values as values
    a = size(s1,"r");   //determining the size for separate tables
    b = size(s1,"c");

    Kx = s1(10,2:b);   //Creation of 3 lists
    Ky = s1(11:a,1);
    K  = s1(11:a,2:b);
endfunction

function [k] = xlookup(target,Searched,r,c,direction, exact)
    //target    - Value searched
    //Searched  - Table to be searched
    //r         - line (row) to be searched
    //c         - columnn to be seached
    //direction - if "h"orizontal or "v"ertical
    //exact     - 0 Exact, 1 Up, -1 Down

    mode(0)
    i=0;
    j=0;

    if direction == "h" then
        for j = 1 : size(Searched , 'c')
            delta(r,j) = abs( target - Searched(r,j) );
        end

        if exact == -1; //down
            k = find(delta == min(delta)) - 1;
        elseif exact == 0; //exact
            k = find(delta == min(delta));            
        elseif exact == 1 //up
            k = find(delta == min(delta)) + 1; 
        end

    elseif direction == "v" then
        for i = 1 : size(Searched , 'r')
            delta(i,c) = abs( target - Searched(i,c) );
        end

        if exact == -1; //down
            k = find(delta == min(delta)) - 1;
        elseif exact == 0; //exact
            k = find(delta == min(delta));            
        elseif exact == 1 //up
            k = find(delta == min(delta)) + 1; 
        end
    end
endfunction

//vertical interpolation
function VI = vinterpol(Target_vi,Searched_vi,r,c,direction,exact)
    VI = zeros(size(Target_vi,'r'),1);
    for i = 1:size(Target_vi, 'r')
        tt = Target_vi(i,c);
        ii = xlookup(tt,Searched_vi,r,c,direction,exact);
        VI(i,c) = Searched_vi(ii,c);
    end
endfunction

//Taking the Headers and Values of the PowerCurve
[K,Kx,Ky] = ImportExcel('WDS_315 PowerCurves.xls',3);

TestW_minus1 = vinterpol(W_WS, Ky,1,1,"v",-1)

问题总是出在这段代码上

VI(i,c) = Searched_vi(ii,c);

函数xlookup对我来说很好用。输出是vinterpol将应用结果的向量中的位置。我想还有其他方法可以基于for中的结果创建一个新向量。谢谢你的帮助。
我尝试使用不同的名称,应用于其他矢量。事实上,当我使用一个小的生成矢量时,它工作正常,但当我使用Excel矢量时,问题重复。

balp4ylt

balp4ylt1#

您的xlookup函数可以简化如下:

function [k] = xlookup(target,Searched,r,c,direction, exact)
    //target    - Value searched
    //Searched  - Table to be searched
    //r         - line (row) to be searched
    //c         - columnn to be seached
    //direction - if "h"orizontal or "v"ertical
    //exact     - 0 Exact, 1 Up, -1 Down

    if direction == "h" then
        [value,k] = min(abs(target - Searched(r,:)));
    elseif direction == "v" then
        [value,k] = min(abs(target - Searched(:,c)));
    end
    k = k + exact;
endfunction

k==1exact==-1时,此函数生成0。Scilab中矩阵的索引从1开始,这解释了在Searched_vi(ii,c)中将其结果用作ii时会出现的错误。

相关问题