Manipulate Arrays Size using Colon Operator.

5 views (last 30 days)
Dmytri
Dmytri on 29 Jan 2014
Commented: Walter Roberson on 29 Jan 2014
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!

Answers (2)

Walter Roberson
Walter Roberson on 29 Jan 2014
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));

Dmytri
Dmytri on 29 Jan 2014
I forgot to mention that I'm trying to make a unit step function. I had thought about using the linspace command but it always creates a linear line in the upward direction. That's why I was trying to use the colon operator to help create this unit step function.
If I don't try to increase the resolution of the plot by increasing the number of points it looks like a heaviside function with the exception of where it switches between the lower and upper number. However, once I add more points in between the upper and lower limits of the time array it becomes less of a heaviside function but rather a positive sloped linear function.
Not sure why this happens...
  1 Comment
Walter Roberson
Walter Roberson on 29 Jan 2014
? 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().

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!