从MATLAB数组只打印连续的数字 [英] Printing only consecutive numbers from an array in matlab

查看:148
本文介绍了从MATLAB数组只打印连续的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB code数组,我想要做的是,
可以说数组是:

I have an array in my matlab code, and what I want to do is, lets say the array is:

wf_array = [1 2 3 4 5 6 7 8 9 10 11  25 26 28 29 45 46 47 48 49 50 51 52 53 54 55 56 57]

预期的输出结果是: 1 2 3 4 5 6 7 8 9 10 11 45 46 47 48 49 50 51 52 53 54 55 56 57

因此​​,O / P应包含数组连续编号,如果系列是连续在-至少10个号码。

Thus the o/p should contain consecutive numbers from that array if the series is consecutive for at-least 10 numbers.

我曾尝试以下

v = wf_array;
x = [0 cumsum(diff(v)~=1)];
final = v(x==mode(x));

但它打印只从数组的最长连续流,其中,因为它应该打印所有这些都没有,至少10个号码连续流。请帮助...

but it prints only the longest consecutive stream from that array where as it should print all the consecutive streams which are there for at least 10 numbers. Please help...

在此先感谢

推荐答案

那么,你的 cumsum 向上计数时,至少有一个值,这不是连续的。这意味着,每个数据流将在 X 不同的号码。 模式,在另一方面,发现只有最频繁的数目,并把它们的最小的一个(因此,如果有相同长度的若干个流,所述第一个将被取)。根据对模式的文档的模式功能不适合有多种模式分布发现高峰。

Well, your cumsum will count up when there is at least one value that's not consecutive. This means, each of your streams will have a different number in x. mode, on the other hand, finds only the most frequent number, and the smallest one of them (so if there are several streams of same length, the first one will be taken). According to the documentation for mode, "the mode function is not suitable for finding peaks in distributions having multiple modes."

因此​​,有3种方法可以解决这个问题。第一个是在一个方式,使所有的流具有相同的编号,以修改x。二是要替换模式的东西,可以找到多种模式。三是采取了完全不同的方法和分裂数组。

So there's 3 ways to tackle this problem. The first is to alter x in a way that makes all streams have the same number. The second is to replace mode with something that can find multiple modes. The third is to take a totally different approach and split the array.

我要第三个,因为我认为这是最简单的。

I'll take the third one because I think it's the "easiest".

w = find( diff(v)~=1 ); %// this contains all indices where the data jumps
x = diff([0 w length(v)]); %// this contains the lengths of the streams
y = mat2cell(v,1,x); %// creates a cell array with each consecutive stream
z = y( cellfun('size',y,2)>=10 ) %// picks those cells with length of at least 10
final = cell2mat(z); %// revert it back to an array

请注意,这个学尝试并不十分可靠,只会在尺寸的1×N 数组。如果您原来的输入数组不同形状,你应该先重塑它:

Note that this attemp is not robust and will only work on arrays of size 1xN. If your original input array is differently shaped, you should reshape it first:

v = input_array_of_arbitrary_size(:)'; %'// reshapes into row vector columnwise

例如,如果你输入

in = [1 4 7
      2 5 8
      3 6 9];

结果将是:

v = in(:)' %'//random comment to fix SO's syntax highlighting
v =

     1     2     3     4     5     6     7     8     9

这适用于任何尺寸的阵列和将在年底追加每个维度。有关详细信息,请直线索引。

This works for arrays of any dimension and will append each dimension at the end. For details, check out linear indexing.

这篇关于从MATLAB数组只打印连续的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆