簡體   English   中英

矩陣作為Matlab函數的輸入和輸出

[英]Matrix as input and output of Matlab function

例如,我想要一個函數,讓我刪除我的矩陣的行,其中最高值為1.因此我寫道:

% A is an input matrix
% pict2 suppose to be output cleared matrix

    function pict2 = clear_(A)
    B=A
    n = size(A,1)
    for i=1:n
      if(max(B(i,:))==1)
        B(i,:)=[]
      end
    end

但在我打電話之后: pict2=clear_(pict) matlab的意思是:

“警告:clear_:返回值列表中的某些元素是未定義的警告:從第5行第1列的clear_調用pict2 =”

我不確定哪些元素未定義?

輸出參數的變量名必須與要返回的變量匹配。 因此,您需要將第一行更改為以下內容,以便保存並返回對B的修改。

function B = clear_(A)

就你的算法而言,它不會起作用,因為你在嘗試循環時修改B 相反,您可以使用以下表達式替換整個函數,該表達式計算每行的最大值,然后確定此值是否等於1並刪除這種情況下的行。

B(max(B, [], 2) == 1, :) == [];

我相信,除了你已經收到的建議,你可能想嘗試以下方法。 使用邏輯可能是解決此類問題的最佳選擇之一,因為您不需要使用for循環:

function out = clear_matr(A)
    % ind is true for all the rows of A, where the highest value is not equal to 1
    ind = ~(max(A, [], 2) == 1);

    % filter A accordingly
    out = A(ind, :);
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM