Matlab 求连续数的中间点

Matlab 求连续数的中间点,matlab,Matlab,假设我有 A=[1 3 4 5 6 7 9 12 15 16 17 18 20 23 24 25 26]; 我的兴趣是如何使用Matlab找到连续数字之间的中间值 例如,第一组连续数字是 B=[3 4 5 6 7]; 所以答案应该是5。第二组连续数字即[15 16 17 18]应给出16等 最后,我的最终答案是 [5 16 24] 下面是一些示例代码,它可以满足您的需要。我将让您使用不同的输出,看看它们到底做了什么,尽管我写了一些评论如下: clear clc A=[1 3 4 5 6

假设我有

A=[1 3 4 5 6 7 9 12 15 16 17 18 20 23 24 25 26];
我的兴趣是如何使用Matlab找到连续数字之间的中间值

例如,第一组连续数字是

B=[3 4 5 6 7];
所以答案应该是5。第二组连续数字即[15 16 17 18]应给出16等

最后,我的最终答案是

[5 16 24]

下面是一些示例代码,它可以满足您的需要。我将让您使用不同的输出,看看它们到底做了什么,尽管我写了一些评论如下:

clear
clc

A=[1 3 4 5 6 7 9 12 15 16 17 18 20 23 24 25 26]

a=diff(A); %// Check the diff array to identify occurences different than 1.

b=find([a inf]>1);
NumElements=diff([0 b]); %//Number of elements in the sequence

LengthConsec = NumElements((NumElements~=1)) %// Get sequences with >1 values
EndConsec = b(NumElements~=1) %// Check end values to deduce starting values
StartConsec = EndConsec-LengthConsec+1;

%// Initialize a cell array containing the sequences (can have ifferent
%lengths, i.e. an array is not recommended) and an array containing the
%median values.
ConsecCell = cell(1,numel(LengthConsec));
MedianValue = zeros(1,numel(LengthConsec));

for k = 1:numel(LengthConsec)

    ConsecCell{1,k} = A(StartConsec(k):1:EndConsec(k));
    MedianValue(k) = floor(median(ConsecCell{1,k}));
end

%//Display the result
MedianValue
提供以下信息:

MedianValue =

     5    16    24

下面是一些示例代码,它可以满足您的需要。我将让您使用不同的输出,看看它们到底做了什么,尽管我写了一些评论如下:

clear
clc

A=[1 3 4 5 6 7 9 12 15 16 17 18 20 23 24 25 26]

a=diff(A); %// Check the diff array to identify occurences different than 1.

b=find([a inf]>1);
NumElements=diff([0 b]); %//Number of elements in the sequence

LengthConsec = NumElements((NumElements~=1)) %// Get sequences with >1 values
EndConsec = b(NumElements~=1) %// Check end values to deduce starting values
StartConsec = EndConsec-LengthConsec+1;

%// Initialize a cell array containing the sequences (can have ifferent
%lengths, i.e. an array is not recommended) and an array containing the
%median values.
ConsecCell = cell(1,numel(LengthConsec));
MedianValue = zeros(1,numel(LengthConsec));

for k = 1:numel(LengthConsec)

    ConsecCell{1,k} = A(StartConsec(k):1:EndConsec(k));
    MedianValue(k) = floor(median(ConsecCell{1,k}));
end

%//Display the result
MedianValue
提供以下信息:

MedianValue =

     5    16    24

以下是一种矢量化方法:

d = [diff(A) == 1, 0];
subs = cumsum([diff(d) == 1, 0]).*(d | [0, diff(d) == -1]) + 1
temp = accumarray(subs', A', [], @median)
final = floor(temp(2:end))

以下是一种矢量化方法:

d = [diff(A) == 1, 0];
subs = cumsum([diff(d) == 1, 0]).*(d | [0, diff(d) == -1]) + 1
temp = accumarray(subs', A', [], @median)
final = floor(temp(2:end))
基于diff+strfind的方法-

loc_consec_nums = diff(A)==1 %// locations of consecutive (cons.) numbers
starts = strfind([0 loc_consec_nums],[0 1]) %// start indices of cons. numbers
ends = strfind([loc_consec_nums 0],[1 0]) %// end indices of cons. numbers
out = A(ceil(sum([starts ; ends],1)./2))%// median of each group of starts and ends
              %// and finally index into A with them for the desired output
基于diff+strfind的方法-

loc_consec_nums = diff(A)==1 %// locations of consecutive (cons.) numbers
starts = strfind([0 loc_consec_nums],[0 1]) %// start indices of cons. numbers
ends = strfind([loc_consec_nums 0],[1 0]) %// end indices of cons. numbers
out = A(ceil(sum([starts ; ends],1)./2))%// median of each group of starts and ends
              %// and finally index into A with them for the desired output

为什么在第二个例子中,中间的数字不是17?你把整数舍入到较低的整数吗?在任何情况下,看起来你在寻找中值,有可能吗?是的,我在寻找中值。如何得到它。准确地说,我有差分A。我得到的结果是[01101101111];然后我卡住了。我不知道如何在连续的数字中找到一个中间点。为什么在第二个例子中中间的数字不是17?你把整数舍入到较低的整数吗?在任何情况下,看起来你在寻找中值,有可能吗?是的,我在寻找中值。如何得到它。准确地说,我有差分A。我得到的结果是[01101101111];然后我卡住了。我不知道如何找到一个连续的中点。@丹乔,如果这已经解决了你的问题或任何其他答案,那么请考虑“丹乔”,如果这已经解决了你的问题或任何其他答案,那么请考虑。