2017-10-31 2 views
0

arrをとり、0と1の数を連続して表示する関数を記述しようとしています。出力は2D配列でなければなりません。列1は何番目のシーケンスで表示され、列2はどのトークンであるか(0または1)です。MATLABモールスコード差分関数と検索関数

arr = [0; 0; 0; 1; 1; 1; 0]; 

が、何もないと

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; 

作品下記の当社の機能は、それがマトリックスにランダムな整数を追加するため。なぜこれを行うのですか?どうすれば修正できますか?ここで

+0

は、あなたが何をしたいの非自明な例を追加あなたがあれば、あなたの結果はarr' 'から垂下したいどうすればよい –

+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];