Manipulate Arrays Size using Colon Operator.
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Walter Roberson
el 29 de En. de 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));
0 comentarios
Dmytri
el 29 de En. de 2014
1 comentario
Walter Roberson
el 29 de En. de 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().
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!