matlab - Make vectors of unequal length equal in length by reducing the size of the largest ones -


i reading 5 columns .txt file 5 vectors.

sometimes vectors 1 element larger others, need check if of equal length, , if not, have find ones largest , delete last element. think should able without loops. thinking of using find in combination isequal isequal returns logical, , not provide information on vectors largest.

[seconds,sensor1vstatic,sensor2vpulsed,temperaturec,relativehumidity] = importfile(path); 

then depending on vectors longer 1 element, do, example

seconds(end) = []; sensor1vstatic(end) = []; 

if seconds , sensor1vstatic longer other vectors 1 element

assume vectors in cell array a:

a = {[1 2 3], [1 2 3 4], [1 2 3 4 5]}; 

you can size of each vector with

sz = cellfun(@(x)size(x,2), a); 

which return (for above example)

sz = [ 3 4 5] 

now can find shortest vector:

minlength = min(sz); 

and finally, make vectors length:

b = cell2mat(cellfun(@(x)x(1:minlength), a, 'uniformoutput', false))'; 

there may more elegant ways (and note cellfun doing "implicit looping")

applying (now expanded) example, assign output of importfile directly cell array - or can separate line:

a = {seconds,sensor1vstatic,sensor2vpulsed,temperaturec,relativehumidity}; 

but becomes lot of work. instead do:

minlength = min(size(seconds,1), size(sensor1vstatic,1), size(sensor2vpulsed,1), ...  seconds = seconds(1:minlength); ... 

there scope cleverness won't make things more readable, , won't save time in long run...


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -