How to do this in Matlab?

1 次查看(过去 30 天)
Shivshankar R
Shivshankar R 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''

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by