MATLAB: Trim RGB Matrix in MatLab

Image Processing ToolboxMATLAB C/C++ Math Librarymatlab codermatrixmatrix manipulation

I have a RGB matrix like this:
0 0 0 0 0 0 0 0
0 0 23 0 0 0 0 0
0 1 255 0 0 130 22 0
0 49 0 0 0 0 120 0
0 0 0 0 79 0 213 0
0 0 0 0 0 0 0 0
and I want to trim this matrix (in other words, remove zeroes at the boundaries) to be like:
0 23 0 0 0 0
1 255 0 0 130 22
49 0 0 0 0 120
0 0 0 79 0 213
And also want to know the middle zero(in other words, longest consecutive of zeros) of every row to be like:
1st row: 4 (There are 5 zeros in the first row. But 4 zeros started at middle point and did not end.)
2nd row: 2 (There are 2 zeros in the second row.)
3rd row: 4 (There are 4 zeros in the third row.)
4th row: 3 (There are 4 zeros in the last row. But 3 zeros finished at middle point.)

Best Answer

You can do
[rows, columns] = find(m > 0);
row1 = min(rows);
row2 = max(rows);
col1 = min(columns);
col2 = max(columns);
m2 = m(row1:row2, col1:col2)