How to do this in Matlab?

1 view (last 30 days)
Shivshankar R
Shivshankar R on 18 May 2016
Commented: Andrei Bobrov on 18 May 2016
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.

Answers (1)

Andrei Bobrov
Andrei Bobrov on 18 May 2016
Edited: Andrei Bobrov on 18 May 2016
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 Comments
Shivshankar R
Shivshankar R on 18 May 2016
Matlab showing 'Undefined function or variable 'x''

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!