find nonzero-middle point of an array

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!

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 1일

1 개 추천

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 5월 1일

2 개 추천

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

Tiger
Tiger 2013년 5월 1일
Thank you Walter, I should have choosen your answer as accepted answer.
Jan
Jan 2013년 5월 1일
@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
Walter Roberson 2013년 5월 1일
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
Azzi Abdelmalek 2013년 5월 1일
If someone asks me to delete my answer, I do not think I will do it.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by