MATLAB Answers


find nonzero-middle point of an array

Asked by Tiger
on 1 May 2013




I want a function that give me middle point of nonzero element which is 4

not 3 which is the middle point of the array.

Thank you!



No products are associated with this question.

2 Answers

Answer by Azzi Abdelmalek
on 1 May 2013
 Accepted answer


Answer by Walter Roberson
on 1 May 2013
 t = nonzeros(a);

floor() cannot be used instead of ceil(): if there was only 1 non-zero element, end would be 1, end/2 would be 1/2, floor(1/2) would be 0, and there is no element #0.


Jan Simon
on 1 May 2013

@Tiger: You can vote for Walter's answer, if it helps you. And you can ask Azzi to delete his answer and resubmit it. Then the status of acceptance is cleared and you can choose again.

Walter Roberson
on 1 May 2013

Azzi's answer will work, as round() of an integer is the integer itself and round() of (integer + 1/2) is the next integer: this is the same set of results as ceil() of the integer and of (integer + 1/2)

Azzi Abdelmalek
on 1 May 2013

If someone asks me to delete my answer, I do not think I will do it.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!