MATLAB: Find maximum number of consecutive negative values

consecutive valuesImage Processing ToolboxMATLABmatrix

Hi,
I have a mxn matrix and I need to find the maximum number of consecutive negative values for each column.
So the output would be a 1xn vector, where the i-th element is the maximum number of consecutive negative values for column "i".
How would I do this without a loop?

Best Answer

A = randi([-6,3],20,15); % Let it your array
t = A < 0;
s = size(t);
tt = cumsum(diff([false(1,s(2));t]) == 1);
[jj,jj] = ndgrid(zeros(1,s(1)),1:s(2));
out = max(accumarray([tt(t),jj(t)],ones(nnz(t),1)));