user466534
user466534

Reputation:

Use zero based matrix in MATLAB

Could the following equation be possible in MATLAB?

Enter image description here

Suppose we have given data of length N, and we want to consider a linear equation among the some L nubmers and find coefficients ai. Is this possible? Because if yes, then coefficients can be solved by

a = pinv(D)*d

where D is a given matrix and d is a left vector.

The above equation comes from the following linear models

Enter image description here

Enter image description here

k = L, L+1, L+2, N-1

I have tested this code with some fixed f.

unction [a] = find_coeficient(y,N,L)
Lp = L + 1;
Np = N + 1;
d = y(L:N-1);
D=[];
for ii=Lp:(Np-1)
    % Index into the y vector for each row of D
    D = vertcat(D, y(ii:-1:(ii-Lp+1))');
end
a = D\d;
end

Is it correct?

Upvotes: 0

Views: 306

Answers (1)

macduff
macduff

Reputation: 4685

This is absolutely possible in MATLAB. However, 0 indexes are not natively supported. You will need to do a "change of variables" by letting the index in each element be index+1. Here's a bit of an example:

% Generate some data
N = 40;
y = 10 * randn(N,1);
% Select an L value
L = N - 4 + 1;
d = y(L:N);
D = reshape(y,4,10);
% Solve the equation using the '\' rather than the pseudo inverse
b = D\d

For more information on the divide operator, see Systems of Linear Equations.

OK, I've thought through this a bit more. Part of the confusion here is the change of variable limits. The substitution applies to the indexing variable, not the size of the data, so L and N are unchanged, but the index is adjusted to keep it from falling off the edge of the array. So in the formula, just add 1 to every element index.

y[L] = [ y[L-1] y[L-2] ... y[0] ] * a1
.
.
y[N-1] = [ y[N-2] y[N-3] ... y[N-L-1] ] * aL

becomes:

y[L+1] = [ y[L-1+1] y[L-2+1] ... y[0+1] ] * a1
.
.
y[N-1+1] = [ y[N-2+1] y[N-3+1] ... y[N-L-1+1] ] * aL

=

y[L+1] = [ y[L] y[L-1] ... y[1] ] * a1
.
.
y[N] = [ y[N-1] y[N-2] ... y[N-L] ] * aL

Which we can then use to complete our script:

function a = find_coeficient(y,N,L)
  d = y((L+1):N);
  D=[];
  for ii=L:(N-1)
    % index into the y vector for each row of D
    D = vertcat(D, y(ii:-1:(ii-L+1))');
  end
  a = D\d;
end

Upvotes: 1

Related Questions