I want to normalize a vector in range -5 to +5. Is it possible?
Show older comments
A vector like v=rand(1,10)*5+1
Answers (2)
Guillaume
on 15 Jun 2015
Note that if all you want is generate random numbers between -5 and 5, it's actually one of the examples in the documentation of rand:
r = -5 + (5+5)*rand(10, 1)
Basically, it is
r = lowerbound + (upperbound - lowerbound) * rand
You can of course rescale your vector to whichever range you want:
vrescaled = (v - min(v)) * (newupper - newlower) / (max(v) - min(v)) + newlower
2 Comments
Aditya Mhetras
on 15 Jun 2015
Guillaume
on 15 Jun 2015
Why couldn't it be? It's a basic linear transformation. As per the formula I've written:
vrescaled = (v - min(v)) * (newupper - newlower) / (max(v) - min(v)) + newlower
where newupper = 5 and newlower = -5, so:
vrescaled = (v - 1) * 10 / 9 - 5;
Andrei Bobrov
on 15 Jun 2015
As wrote Guillaume:
variant 1:
out = A - 5; % if A >= 0 & A<=10
variant 2:
m = min(A(:));
out = (A(:)-m)/(max(A)-m)*10-5;
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!