3

For example I want to have a function which let me delete rows of my matrix where the highest value is 1. Thus I wrote:

% 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

But after I call: pict2=clear_(pict) the Matlab resposes:

"warning: clear_: some elements in list of return values are undefined warning: called from clear_ at line 5 column 1 pict2 = "

I'm not sure which elements were left undefined?

3
  • Yes. That's what you want. But look at the second part of my comment since what you're trying to do isn't actually possible. Commented Sep 28, 2016 at 22:15
  • Well, I changed first line for function B = clear_(A) and there is no error. However it is still doesn't work as I want to. Commented Sep 28, 2016 at 22:17
  • You can replace this entire function with: B(max(B, [], 2) == 1, :) = []; Commented Sep 28, 2016 at 22:18

2 Answers 2

2

The variable name of your output argument must match the variable that you want to be returned. So you'll want to change the first line to the following so that your modifications to B are saved and returned.

function B = clear_(A)

As far as your algorithm is concerned, it's not going to work because you are modifying B while trying to loop through it. Instead, you can replace your entire function with the following expression which computes the maximum value of each row, then determines if this value is equal to 1 and removes the rows where this is the case.

B(max(B, [], 2) == 1, :) == [];
Sign up to request clarification or add additional context in comments.

Comments

0

I believe that, alternatively to the suggestions you already recieved, you might want to try the following. Using logicals is probably one of the best options for such a problem, since you don't need to use for-loops:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.