Manipulate Arrays Size using Colon Operator.
조회 수: 13 (최근 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!