【问题标题】:Return and call multiple value from function从函数返回并调用多个值
【发布时间】:2019-12-11 17:05:40
【问题描述】:

我试图从我的函数中返回 for 循环的所有值,并且我需要在函数之外调用它们。但我只得到 for 循环的最后一行,我还需要 for 循环的先前结果。 这是我的代码

i=input('Enter a start row: ');
j=input('Enter a end row: ');
c=input('Enter classifier variable column number:')


search= importfiledataset('search-queries-features.csv',i,j);

[n,p]=size(search);

 if j>n
        disp('Please enter a smaller number!');
 end
  [D1,Eps1]=findD(i,j,c,search);
   disp(D1);
   disp(n1);

function [D1,Eps1] = findD(i,j,c,search)      %Find D vlaues with for loop for each classification value 

    for numOfClassifier = 1 : 100
        a = search(search(:,c)==numOfClassifier,:) ; 
        q1 = a(all(~isnan(a),2),:); % for nan - rows
        D1 = a(:,all(~isnan(a)))  % for nan - columns WE FIND D1

        n1=size(D1,1) %number of record belongs to classification

        sampleSpace = size(search,1) %sample space of the priop probability(#of c1 + #of c2 .... cn)

        pc1 = n1/sampleSpace %prior probability of the n

        mu1 = mean(D1) %mean of D1

        Z1 = D1 - mu1 % centered data of D1

        Eps1 = (1/n1)*(transpose(Z1)*Z1) %covariance matrix if Z1


        numOfClassifier = numOfClassifier + 1;
        if search(:,c) ~= numOfClassifier
            break
        end 
    end  
end

我要退货

  • D1
  • Eps1

我想返回 for 循环的全部值,但我只得到最后一行的值。

【问题讨论】:

标签: matlab for-loop return return-value function-call


【解决方案1】:

您可以将迭代结果存储在cell array 中,并返回元胞数组:

函数返回两个元胞数组(我将它们命名为allD1allEps1):

function [allD1, allEps1] = findD(i,j,c,search)

在循环之前将单元格数组初始化为空单元格:

allD1 = {};
allEps1 = {};

D1Eps1 添加到元胞数组的末尾(将其放在带有break 语句的行之前):

allD1{end + 1} = D1;
allEps1{end + 1} = Eps1;

这里是修改后的代码:

i=input('Enter a start row: ');
j=input('Enter a end row: ');
c=input('Enter classifier variable column number:')


search= importfiledataset('search-queries-features.csv',i,j);

[n,p]=size(search);

 if j>n
        disp('Please enter a smaller number!');
 end
  [D1,Eps1]=findD(i,j,c,search);
   disp(D1);
   disp(n1);

function [allD1, allEps1] = findD(i,j,c,search)      %Find D vlaues with for loop for each classification value 
    %Initialize empty cell arrays
    allD1 = {};
    allEps1 = {};

    for numOfClassifier = 1 : 100
        a = search(search(:,c)==numOfClassifier,:) ; 
        q1 = a(all(~isnan(a),2),:); % for nan - rows
        D1 = a(:,all(~isnan(a)))  % for nan - columns WE FIND D1

        n1=size(D1,1) %number of record belongs to classification

        sampleSpace = size(search,1) %sample space of the priop probability(#of c1 + #of c2 .... cn)

        pc1 = n1/sampleSpace %prior probability of the n

        mu1 = mean(D1) %mean of D1

        Z1 = D1 - mu1 % centered data of D1

        Eps1 = (1/n1)*(transpose(Z1)*Z1) %covariance matrix if Z1

        %Add D1 (and Eps1) to the end of the cell array
        allD1{end + 1} = D1;
        allEps1{end + 1} = Eps1;

        numOfClassifier = numOfClassifier + 1;
        if search(:,c) ~= numOfClassifier
            break
        end 
    end  
end

元胞数组比数组更通用 - 元胞可以存储不同类型和形状的值,这与所有元素必须属于同一类型的数组相反。

在您的情况下,多维数组可能适合,但更令人困惑。

对于元胞数组,每个元胞都保存匹配迭代的结果。
例如:allD1{3} 在第三次迭代中保存D1 的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多