find nonzero-middle point of an array
1 view (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
More Answers (1)
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.
4 Comments
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)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!