How to calculate the n period Element difference every in a matrix ?

조회 수: 4 (최근 30일)
Charles
Charles 2019년 4월 4일
답변: Image Analyst 2019년 4월 4일
Hello. I have a matrix A, of size n x m, and second matrix of prices, matrix B same size.
I wish to find the difference between each element n periods apart in each colum of matrix A So I start from the last row and subtract the element 19 rows prior. This window should then move up one,.....a moving window
I then want to divide the resulting matrix of differences by the equivalent row of matrix B. Matrix B will of course be same dimension as resulting matrix. So we index into matrix B to extract a matrix which is 19 rows short from the top.
To do part 1, I found this and I am trying to understand and adapt it. I feel I could use the arrayfun function...but not 100 per cent sure how.
N = 19; diffmatrix = arrayfun(@(i) diff(a(i:i+n+1), a(i:i+n-1))

답변 (1개)

Image Analyst
Image Analyst 2019년 4월 4일
Use conv() with a kernel that's like a column vector of [1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;-1] (start with 1, then 17 zeros, then -1). Use the 'valid' option.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by