How to do this in Matlab?

1 回表示 (過去 30 日間)
Shivshankar R
Shivshankar R 2016 年 5 月 18 日
コメント済み: Andrei Bobrov 2016 年 5 月 18 日
I have a following code
x=randi([1 150],1,400);
d=max(x);
m=numel(x);
y=zeros(d,1);
p=zeros(d,d);
for k=1:m-1
y(x(k))=y(x(k))+1;
p(x(k),x(k+1))=p(x(k),x(k+1))+1;
end
p=bsxfun(@rdivide,p,y);
p(isnan(p)) = 0;
j=prod(p(p~=0));
[~,~,idx] = unique(x);
q=prod(hist(idx,1:max(idx))/numel(x));
s=log(j);
l=log(q);
g=s+l
In above code random no. generated from 1 to 150 with sequence length 400. finally I am getting output g,suppose random no. sequence generated by code is
x=[5,96,96,55,55,65,65,65,12,12,6,7,9,9,9,31,31,14,26,26,26,101,101,145,145,...]
so code is given output for whole sequence, but i want output for sliding window of size 10, i.e. o/p for sequence
[5,96,96,55,55,65,65,65,12,12],
then skipping 1st element and adding next element so window size should remain same, i.e.
[96,96,55,55,65,65,65,12,12,6],
then next for
[96,55,55,65,65,65,12,12,6,7],
likewise up to last, how to do it.

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2016 年 5 月 18 日
編集済み: Andrei Bobrov 2016 年 5 月 18 日
x0 = [4,1,1,1,2,2,3,5,9,7,7,7,6,6,1,1,2,3,4,4];
n = 10;
m = numel(x0) - n + 1;
x1 = hankel(x0(1:end-n+1),x0(n+1:end));
g = zeros(m,1);
for jj = 1:m
x = x1(jj,:);
y = accumarray(x(:),1);
p = accumarray(hankel(x(1:end-1),x(end-1:end)),1);
p = bsxfun(@rdivide,p,y);
p(isnan(p)) = 0;
g(jj) = log(prod(y(y~=0)/numel(x))*prod(p(p~=0)));
end
  2 件のコメント
Shivshankar R
Shivshankar R 2016 年 5 月 18 日
Matlab showing 'Undefined function or variable 'x''
Andrei Bobrov
Andrei Bobrov 2016 年 5 月 18 日
corrected

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by