Asked by Tiger
on 1 May 2013

Hello,

example

a=[0,0,3,4,5];

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.*

Answer by Azzi Abdelmalek
on 1 May 2013

Accepted answer

idx=find(a); out=a(idx(round(numel(idx)/2)))

Answer by Walter Roberson
on 1 May 2013

t = nonzeros(a); t(ceil(end/2))

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.

Show 1 older comment

Walter Roberson
on 1 May 2013

Azzi Abdelmalek
on 1 May 2013

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

Opportunities for recent engineering grads.

## 0 Comments