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 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