How to make time shift in a vector using non integer values?

I need to implement this equation
I(t-Ts)= co*n(t) +c1* n(t-Ts)
Where Ts is 1 nanosecond, so I keep getting an error " Array indices must be positive or logical values". How can I implement that shift in time? Thank you.

2 Comments

Most likely you are confusing data (e.g. time) with code (e.g. indices). Please show your code.
yes, i'm iterating on t please see the piece of code:
for t =1:10000
nc(t)= sqrt(d0*input(t)+d1)+d2;
I(t)=co*nc(t)+c1*nc(t-Ts)+c2*(nc(t-Ts)^2)+c3*(nc(t-Ts)^3);
end
where Ts is the sampling time choosen to be 1ns, and input is 1x10000 vector

Sign in to comment.

 Accepted Answer

I(t-Ts)= co*n(t) +c1* n(t-Ts)
I'm guessing that t has a value of 1 at some point and ts=1 nanosecond, so (t-ts)=0 at some point. As the error message indicates, the array indices must be positive values (or logical values).
Other possibilities are that (t-ts) results in a positive non-integer value or a negative value, both of which cannot be used as indices.
Note that this index problem happens with I(t-Ts) and with n(t-Ts). Without knowing more about what you're doing, I can't provide a more specific solution.

6 Comments

yes, i'm iterating on t please see the piece of code:
for t =1:10000
nc(t)= sqrt(d0*input(t)+d1)+d2;
I(t)=co*nc(t)+c1*nc(t-Ts)+c2*(nc(t-Ts)^2)+c3*(nc(t-Ts)^3);
end
where Ts is the sampling time choosen to be 1ns, and input is 1x10000 vector
thank you
Looks like my answer was correct.
On the first iteration, t=1 and ts=1 so (t-Ts)=0
In Matlab, indices start with 1 unlike other languages that start with 0.
so is there a work around to fix this and implemet the wanted equation? also Ts is 10^(-9)
10^-9 is a lot different than 1(ns). Providing this level of detail from the start is important and will save lots of time.
Indexing requires positive integers. For example, if 'a' is an array, a(j) is interpreted as the j'th value in a. If j=4, a(4) is the 4th value.
Here's another example, b=[1,2,3,4]; what's the 0.004'th value of b? That question doesn't even make sense. Neither does "nc(t-Ts)".
> is there a work around to fix this and implemet the wanted equation
To answer that, we need to understand what you intended to do with the equation and until mind-reading technology becomes available, we cannot answer that question.
I can make a guess and suggest that you wanted this,
for t =1:10000
nc(t)= sqrt(d0*input(t)+d1)+d2;
I(t)=co*nc(t)+c1*nc(t)-Ts+c2*(nc(t)-Ts)^2+c3*(nc(t)-Ts)^3;
end
but ultimately you have to 1) understand what the line is supposed to be doing and then 2) determine if it's doing that.
It might be helpful to see this color-coded version of how I re-grouped the variables.
Okay, I understand better now, thank you
Glad I could point you in the right direction. If this answered your question, please consider accepting the answer (blue "accept this answer" button). If you have any other questions on this topic, let us know.

Sign in to comment.

More Answers (0)

Asked:

on 11 Jun 2021

Edited:

on 14 Jun 2021

Community Treasure Hunt

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

Start Hunting!