MATLAB: Output a fixed-length vector from a random-length vector? -


say have randomly-length vector:

a = [1 2 3 4 5 6 7 8 9 91 1 3 2 4 2 7 6 9 12 313 32 57 327 2 13]  b = [1 9 3 8 5 6 7 8 9 91 1 3 2 4 2 7 6 9 12 313 32 57 327 2 13 1 1 1 1 1 1 0 0 0 0 0] 

i want output vector of length 10, evenly spaced element (including first , last element). know have use interpolation if original vector not of length multiples of 10. how can it?

if understand question correctly want use interpolation produce vector of 10 elements input vector. following code should work.

b = [1 9 3 8 5 6 7 8 9 91 1 3 2 4 2 7 6 9 12 313 32 57 327 2 13 1 1 1 1 1 1 0 0 0 0 0] interp1(linspace(1,10,length(b)),b,1:10) 

output:

ans =      1.0000    5.3333    8.7778    2.3333    6.4444  188.1111    5.6667    1.0000         0         00         0 

basically, define b values defined on range of 1 10 , resample on 10 element vector 1:10. if need integers instead of doubles can use round.

b = [1 9 3 8 5 6 7 8 9 91 1 3 2 4 2 7 6 9 12 313 32 57 327 2 13 1 1 1 1 1 1 0 0 0 0 0] round(interp1(linspace(1,10,length(b)),b,1:10)) 

output:

ans =       1     5     9     2     6   188     6     1     0     0 

hope helps.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -