필터 지우기
필터 지우기

Finding the "center integers" of an array

조회 수: 1 (최근 30일)
Ben
Ben 2015년 5월 25일
답변: Alfonso Nieto-Castanon 2015년 5월 25일
Consider the input vector:
[1 2 3 10 11 12 15 16 17 20 21 22 23 24 30]'
Let's group the elements of the input vector in the following manner:
[1 2 3] [10 11 12] [15 16 17] [20 21 22 23 24] [30] (This is not part of the output)
Then the desired output is the center integer of these "groupings":
[2 11 16 22 30]
Is there a quick and easy way to do this with only a few functions? (Preferably without a for-loop). Let's assume these integer "groupings" contain an odd number of elements so there is always one center integer.
I have no starting code unfortunately. I considered using the diff command, but I got stuck from there.
Let me know if I need to clarify anything. Thank you for your help.

채택된 답변

Walter Roberson
Walter Roberson 2015년 5월 25일
t = find([true, diff(X)>1, true]);
Output = X((t(1:end-1) + t(2:end)-1)/2);

추가 답변 (2개)

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2015년 5월 25일
x = [1 2 3 10 11 12 15 16 17 20 21 22 23 24 30];
y = x(floor(convn(find(diff([nan x nan])~=1),[1 1]/2,'valid')));

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2015년 5월 25일
x = [1 2 3 10 11 12 15 16 17 20 21 22 23 24 30];
y = cellfun(@mean,mat2cell(x, 1, diff(find(diff([nan x nan])~=1))));

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by