i have the observation z(1),z(2),..., z(n) a numerical values and have B is called back shift operator . when B intervention on any observation z(n) give me the previous observation as following
1 次查看(过去 30 天)
显示 更早的评论
回答(1 个)
John D'Errico
2017-1-8
It surely depends on what you need it for. For example, I can think of ways to do this, that would be terrible, and other ways you might try it that would not be terribly efficient.
For example, you might simply want to write a trivial function:
B = @(z,n,t) z(n-t);
Or, you could write an m-file function that would be more intelligent, supplying a default value for t, such that if t is not supplied, then the default for t is 1. So you might write
function znt = B(z,n,t)
% simple back shift operator
% arguments:
% z - vector to be operated upon
% n - index of the element of z
% t - offset (optional), default = 1
if (nargin < 3) || isempty(t)
t = 1;
end
if any(n-t) <= 0
error('The sky is falling. Index with negative or 0 called for.')
end
% just do the index operation
znt = z(n-t);
You could be even ncieer
All this is just simple vector indexing. And in fact, there is little real reason to need such a function, as long as you understand indexing.
My fear is that you want this because you have some general formula written with such an operator, and that you will next just expect MATLAB to be able to solve for the vector z in some magical way.
The fact is, MATLAB does NOT "understand" such a function B. Yes, it is trivial to define it as I did above. But computers don't understand anything. They do only what they are told. It is YOU who must supply the understanding, the context under which such a function was written and will be used.
So MATLAB cannot understand that this operator applies to some general index n, of the vector z. For example, you might think to use this operator on the Fibonacci sequence, perhaps like this.
F(n) = B(F,n,1) + B(F,n,2)
Sorry, but MATLAB will not then be able to magically solve for the vector F. Yes, you could put the above expression in a loop, and get it to work. Even then, be careful, since you want to preallocate F for efficiency.
B = @(z,n,t) z(n-t);
F = zeros(1,20);
F(1) = 1;
F(2) = 1;
for n = 3:20
F(n) = B(F,n,1) + B(F,n,2);
end
F
F =
Columns 1 through 13
1 1 2 3 5 8 13 21 34 55 89 144 233
Columns 14 through 20
377 610 987 1597 2584 4181 6765
As you can see, this did produce the Fibonacci sequence. But I had to write it as an explicit loop. In fact, the function B that I defined was silly there, since I could far more trivially written this line inside the loop:
F(n) = F(n-1) + F(n-2);
The point is, this operator that you so much want to see created is in fact completely useless. Worse, the code to compute the Fibonacci sequence as I wrote it will be more efficient if I never used the B operator at all.
In the end, I think you don't really need this operator. Instead, you need to learn to program, without blindly trying to implement a formula as you read it, but understand what the formula means, and then implement it in code.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!