Mazzy
Mazzy

Reputation: 14179

Improve the performance of this code

Suppose I have a matrix called A:

3 0 3 3
0 0 4 0

And I have this function:

while(1)
  if all(A(:,i) == 0)
    i = i + 1;
  else
    i = i + 1
    break;
  end
end

How can I improve the performance of this code?

Upvotes: 1

Views: 58

Answers (1)

Jonas
Jonas

Reputation: 74930

Very easily:

i = sum(all(A==0,1),2)

Upvotes: 3

Related Questions