Rounding each individual value in a vector to values in another vector
    7 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
If you have v1 = [2.5 3.74 7.92] (These numbers are meant to be random) and you wanted to round these numbers up or down, you use floor(v1) or ceil(v1). But what if you wanted to round these numbers to other values instead of integers? Say you want to round them to the values in vector v2 = 1.25:10.25. How would you go about doing it without using a for loop?
Such that:
"function"(v1,v2) = [2.25 3.25 7.25] %Rounding to the left "function"(v1,v2) = [3.25 4.25 8.25] %Rounding to the right
I know that one method is
for i = 1:length(v1) v2(find(v1(i)-v2>0,1,'last')); % Nearest lower end
for i = 1:length(v1) v2(find(v1(i)-v2<0,1,'first')); % Nearest higher end
However, this method involves a for loop. Is there a more elegant method with only vectorization involved?
Thanks for your help.
0 commentaires
Réponse acceptée
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Logical dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

