Repeating a vector up to a given length
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
This is one of those case where I feel there has to be a neater way to do what seems like a very simple operation, but I can't think of it.
I want to extend a vector up to some new size by repeating that vector (or some part of the vector starting at the beginning for the final incomplete repitition). Now this gives me the correct result:
traceData = rand(277,1);
len = 4096;
extendedTraceData = repmat( traceData, ceil( len / numel( traceData ) ), 1 );
extendedTraceData = extendedTraceData( 1:len );
but it looks a bit ugly. Am I missing some 1 line function that can do this since repmat only allows full replications of a vector rather than partial ones.
0 commentaires
Réponse acceptée
Jan
le 21 Nov 2016
Modifié(e) : Jan
le 21 Nov 2016
X = rand(277,1);
len = 4096;
extendedX = [repmat(X, floor(len / numel(X)), 1); ...
X(1:mod(len, numel(X)))];
Or:
extendedX = X(mod(0:len-1, numel(X)) + 1);
4 commentaires
Jan
le 22 Nov 2016
Modifié(e) : Jan
le 22 Nov 2016
But note, that the 2nd answer creates a long temporary vector, which is used for indexing. This means that each elemt is checked if it is inside the boundaries and this wastes a lot of time.
Therefore the first method should be faster. But even here the two parts will be created as temporary arrays at first.
Plus de réponses (2)
Guillaume
le 21 Nov 2016
Modifié(e) : Guillaume
le 21 Nov 2016
Unless there's something hidden in some toolbox, I don't think that there is any matlab function to do that in just one line. Replicate and crop as you've done looks like the most efficient solution.
... until matlab supports indexing the return value of a function.
0 commentaires
Image Analyst
le 21 Nov 2016
Regarding "or some part of the vector starting at the beginning".........To replicate just the beginning portion of your vector, use colon. For example to replicate only the first 100 elements of the 277, do this:
extendedTraceData = repmat(traceData(1:100),......
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
