How to output a value of an array for a specific value of time?

I have the following section of code. I am generating arrays of t and v to create a plot.
wn and theta are given.
n=3; N=500; t=linspace(0,n*Tn,N); v=rho*cos(wn*t+theta);
Now how do I print the value of v for t=1?

回答 (1 件)

David Sanchez
David Sanchez 2014 年 9 月 18 日

0 投票

idx = find(t==1);
v(idx)
or in a single line:
v( find(t==1) )

5 件のコメント

David Sanchez
David Sanchez 2014 年 9 月 18 日
or just
v( t==1 )
Abhinav Prashant Mohanakrishnan
Abhinav Prashant Mohanakrishnan 2014 年 9 月 18 日
What happens if 1 is not a part of the array 't'?
David Sanchez
David Sanchez 2014 年 9 月 18 日
you will get an empty matrix
>> v(t==1)
ans =
Empty matrix: 1-by-0
Abhinav Prashant Mohanakrishnan
Abhinav Prashant Mohanakrishnan 2014 年 9 月 18 日
編集済み: Abhinav Prashant Mohanakrishnan 2014 年 9 月 18 日
That's exactly my problem. I generated 't' to plot a displacement vs time graph but I also need the displacement value at time t. So now that 't' is an empty vector, I am not able to print anything. Is there a way to goal seek using linspace? Somehow include 1 in the series?
David Sanchez
David Sanchez 2014 年 9 月 18 日
Absolutely, as explained by Daniel in http://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector you can do this:
t=linspace(0,n*Tn,N);
idx = find(t<1,1);
insert = @(a, x, n)cat(2, x(1:n), a, x(n+1:end)); % function to insert the value "a" in array "x" in position "n"
t = insert(1, t, idx); % now t contains the value "1"
v=rho*cos(wn*t+theta);

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by