Manipulate Arrays Size using Colon Operator.
10 ビュー (過去 30 日間)
古いコメントを表示
I am having trouble using the colon operator properly. I have two variables which will be plotted using the plot command. However, to plot this graph I need each of the vector arrays to have the same number of elements. Below is how I've used the colon operator.
time = min(timeInterval) : 0.001 : max(timeInterval);
signal = min(origSignal) : (max(origSignal) - min(origSignal))/length(time) : max(OrigSignal);
I keep running into the issue that the vector arrays time and signal are not of the same size. Signal is always 1 element smaller than time regardless of what the increment specified in the time array declaration. Any thoughts on how to manipulate the number of elements? Either add a value to the end of the array or delete a value from the end of the larger array.
Thanks for any assistance!
0 件のコメント
回答 (2 件)
Walter Roberson
2014 年 1 月 29 日
Consider:
Let both of them be 1:10, which is length(10). min() is 1, max is 10, 10-1 is 9, so the increment becomes 9 / 10. Then 1 : 9/10 : 10 is
1, 19/10, 28/10, 37/10, 46/10, 55/10, 64/10, 73/10, 82/10, 91/10, 10
and that's 11 elements. What increment should be applied, 1 : x : 10 to get 10 elements? Clearly it should be 1.
Look at it algebraically. If the max is to be the t'th element, then min + increment * (t-1) = max, so increment * (t-1) = max-min so increment = (max-min)/(t-1)... but you are dividing by t instead of by t-1.
You should consider using
signal = linspace( min(origSignal), max(origSignal), length(time));
0 件のコメント
Dmytri
2014 年 1 月 29 日
1 件のコメント
Walter Roberson
2014 年 1 月 29 日
? The colon operator is always going to produce a monotonic increasing or decreasing list. You cannot use 0 as the increment. If you need a bunch of values repeated then use repmat().
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!