需要分析一组包含0和正整数的向量

发布于 2024-12-08 16:24:40 字数 422 浏览 0 评论 0原文

我正在使用 Matlab,并且有一个 1x200 的数字向量。

我需要按照以下规则为这组数字分配一个“分数”:

  1. 如果有 2 个或 3 个或 4 个连续正数,则 0.5 分
  2. 如果有 5 个或更多连续正数,则 1.0 分
  3. 如果没有t 任何连续的正数,例如:0 0 0 6 0 0,则为 0.0 点。 (忽略它,将正数视为零)
  4. 如果一串正整数中间只有一个零,则忽略该零(将其视为正整数)
  5. 如果有两个或多个连续的零,这打破了连续正数的运行。

示例: 30 43 54 0 0 0 41 54 14 10 1 0 0 0 0 32 41 98 12 0 0 0 (总共 2.0 分)

最后,应该有一个分数统计。

对于此类问题有什么有用的函数吗?

I am using Matlab, and I have a 1x200 vector of numbers.

I need to assign a "score" to the set of numbers by following these rules:

  1. If there are 2 or 3 or 4 consecutive positive numbers, then 0.5 points
  2. If there are five or more consecutive positive numbers, then 1.0 points
  3. If there isn't any consecutive positive number, for example: 0 0 0 6 0 0, then 0.0 point. (ignore it, consider that positive number as zero)
  4. If there is only one zero in the middle of a run of positive integers, then ignore that zero (consider it as a positive integer)
  5. If there are two or more consecutive zeroes, that breaks the run of consecutive positive numbers.

Example: 30 43 54 0 0 0 41 54 14 10 1 0 0 0 0 32 41 98 12 0 0 0 (2.0 points total)

At the end, there should be a tally of the points.

Are there any useful functions for this type of problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我恋#小黄人 2024-12-15 16:24:40

这是基于我对这个问题的理解,正如我在上面的问题中所指出的。我已经“取消抑制”所有输出,因此您可以看到发生了什么。

%Rules:
%1. If there are 2 or 3 or 4 consecutive positive numbers, then 0.5 point
%2. If there are five or more consecutive positive numbers, then 1.0 point
%3.  And if there isn't any consecutive positive number, for example: 
%   0 0 0 6 0 0, then 0.0 point. (ignore it, consider that positive 
%   number as zero)
%4. if there is only one zero in the middle of positive integers = ignore 
%   that zero (consider it as a positive integer)
%5. If there are two or more consecutive 0, THEN no point.

%testData = [0 30 43 54 0 0 0 41 54 14 10 1 0 0 0 0 32 41 98 12 0 0 0 1 2 0 1 2 0 ];
testData = [30 43 54 0 0 0 41 54 14 10 1 0 0 0 0 32 41 98 12 0 0 0 ];
posa = testData>0;
%add 0s at each end so that the diffs at the ends work.
diffa = diff([0 posa 0])
starts = find(diffa ==1)
ends = find(diffa==-1)

% Rule 4 if any end (-1) is immediately followed by a start, that means that there 
%   is a 0 in the middle of a run.  substitute a 1 in the position and recalc.
midZeroLengths = starts(2:end) - ends(1:(end-1));
%pad to account for the fact that we only compared part.
midZeroLengths = [midZeroLengths 0];
if any(midZeroLengths == 1);
    testData(ends(midZeroLengths==1)) = 1;
    posa = testData>0;
    %add 0s at each end so that the diffs at the ends work.
    diffa = diff([0 posa 0])
    starts = find(diffa ==1)
    ends = find(diffa==-1)  
end

runs = ends-starts
halfs = (runs > 1) & (runs < 5)
wholes = (runs > 4)
final = sum(halfs)*0.5 + sum(wholes)

This is based on my understanding of the question, as noted in my question above. I've "unsuppressed" all output, so you can see what's going on.

%Rules:
%1. If there are 2 or 3 or 4 consecutive positive numbers, then 0.5 point
%2. If there are five or more consecutive positive numbers, then 1.0 point
%3.  And if there isn't any consecutive positive number, for example: 
%   0 0 0 6 0 0, then 0.0 point. (ignore it, consider that positive 
%   number as zero)
%4. if there is only one zero in the middle of positive integers = ignore 
%   that zero (consider it as a positive integer)
%5. If there are two or more consecutive 0, THEN no point.

%testData = [0 30 43 54 0 0 0 41 54 14 10 1 0 0 0 0 32 41 98 12 0 0 0 1 2 0 1 2 0 ];
testData = [30 43 54 0 0 0 41 54 14 10 1 0 0 0 0 32 41 98 12 0 0 0 ];
posa = testData>0;
%add 0s at each end so that the diffs at the ends work.
diffa = diff([0 posa 0])
starts = find(diffa ==1)
ends = find(diffa==-1)

% Rule 4 if any end (-1) is immediately followed by a start, that means that there 
%   is a 0 in the middle of a run.  substitute a 1 in the position and recalc.
midZeroLengths = starts(2:end) - ends(1:(end-1));
%pad to account for the fact that we only compared part.
midZeroLengths = [midZeroLengths 0];
if any(midZeroLengths == 1);
    testData(ends(midZeroLengths==1)) = 1;
    posa = testData>0;
    %add 0s at each end so that the diffs at the ends work.
    diffa = diff([0 posa 0])
    starts = find(diffa ==1)
    ends = find(diffa==-1)  
end

runs = ends-starts
halfs = (runs > 1) & (runs < 5)
wholes = (runs > 4)
final = sum(halfs)*0.5 + sum(wholes)
段念尘 2024-12-15 16:24:40

怎么样:

str = repmat('a', 1, numel(testData));
str(testData > 0) = 'b';
m = regexp(str, 'b+(ab+)*', 'match');
n = cellfun(@numel, m);
score = 0.5 * sum(n >= 2 & n <= 4) + 1.0 * sum(n >= 5);

请注意,我还没有运行过这个,所以可能会有错误。

How about:

str = repmat('a', 1, numel(testData));
str(testData > 0) = 'b';
m = regexp(str, 'b+(ab+)*', 'match');
n = cellfun(@numel, m);
score = 0.5 * sum(n >= 2 & n <= 4) + 1.0 * sum(n >= 5);

Note that I haven't run this, so there may be errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文