Reputation: 49
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?
Upvotes: 3
Views: 810
Reputation: 65460
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, :) == [];
Upvotes: 2
Reputation: 945
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
Upvotes: 0