gpt4 book ai didi

java - 需要分析一组包含0和正整数的 vector

转载 作者:行者123 更新时间:2023-12-01 05:38:46 25 4
gpt4 key购买 nike

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

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

  1. 如果有 2 个或 3 个或 4 个连续正数,则 0.5 分
  2. 如果有五个或更多连续正数,则 1.0 分
  3. 如果没有连续的正数,例如: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 分)

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

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

最佳答案

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

%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)

关于java - 需要分析一组包含0和正整数的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7678666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com