我想知道每一段时间里革命的次数。例如,第一阶段的革命次数是3次,第二阶段的革命次数又是3次,但不一定每个时期的革命次数是相同的。请参阅示例:

我试过使用for循环,但它可以工作一段时间,有什么办法可以帮助我吗?
x = 0:33;
y1 = repmat([0 1].',17,1);
y2 = [0; 0; 0; 0; 0; 0; 5; 5; 5; 5; 5; 5; 5; 0; 0; 0; 0; 0; 0; 0;...
5; 5; 5; 5; 5; 5; 5; 0; 0; 0; 0; 0; 0; 0];换句话说,我如何才能知道y1在y2的每一个周期中的总数量,当y2==5
find(y1(:,:)==1&y2==5)发布于 2016-10-05 15:30:57
这里有一个想法:
x = 0:33;
y1 = repmat([0 1].',17,1);
y2 = [0; 0; 0; 0; 0; 0; 5; 5; 5; 5; 5; 5; 5; 0; 0; 0; 0; 0; 0; 0;...
5; 5; 5; 5; 5; 5; 5; 0; 0; 0; 0; 0; 0; 0];
d = diff([y2(1) y2.']); % find all switches between diferent elements
len = 1:numel(y2); % make a list of all indices in y2
idx = [len(d~=0)-1 numel(y2)]; % the index of the end each group
counts = [idx(1) diff(idx)]; % the number of elements in the group
elements = y2(idx); % the type of element (0 or 5)
n_groups = numel(idx); % the no. of groups in the vector
rev = zeros(sum(elements==5),1);
c = 1;
for k = 1:n_groups
if elements(k)==5
rev(c) = sum(y1(idx(k)-counts(k)+1:idx(k)));
c = c+1;
end
end结果是:
rev =
3
3https://stackoverflow.com/questions/39873511
复制相似问题