我们正在尝试编写一个带有arr的函数,并计算序列中出现的0和1的数量。输出应该是2D数组,其中第1列是顺序出现的数量,第2列是哪个标记(0或1)。我们的职能如下
function [token] = tokenizeSignal(arr)
matA = diff(find(diff([log_vector;-1])));
addA = zeros(size(matA, 1),1);
matA = [matA, addA];
matB = diff(find(diff([log_vector;0])));
addB = ones(size(matB, 1), 1);
matB = [matB, addB];
[nRowsA, nCols] = size(matA);
nRowsB = size(matB, 1);
AB = zeros(nRowsA + nRowsB, nCols);
AB(1:2:end, :) = matA;
AB(2:2:end, :) = matB;
token = AB;
与
一起使用arr = [0; 0; 0; 1; 1; 1; 0];
但没有其他因为它将随机整数添加到矩阵中。为什么会这样做,我该如何解决?
答案 0 :(得分:0)
以下代码采用任何数组arr并生成您想要的内容:
% input checking/processing
% ... convert the input into a column vector
arr = arr(:);
% ... check that the input is nonempty and numeric
if ~isnumeric(arr), error('Bad input'); end
if isempty(arr), error('Bad input'); end
% determine the starting indices of each sequence in arr
I = [1 ; find(diff(arr)) + 1];
% determine the values of each of these sequences
values = arr(I);
% determine the length of each of these sequences
value_counts = [diff(I) ; length(arr) - max(I) + 1];
% produce the output
token = [value_counts, values];