Round random vector to nearest number in a fixed vector

2 Ansichten (letzte 30 Tage)
Kelvin
Kelvin am 19 Jan. 2015
Kommentiert: Kelvin am 19 Jan. 2015
My question is as follows: I get some random measured values and I want to round them to a scale I have predefined. One solution to this is:
%I have a fixed Cooling Capacity that has the following values:
CoolingCapacity = [2,5,10];
%And a vector with these measured values
CoolingC = [1,2.3,5.3,7.2,10.2,12.2];
if CoolingC < 2.5
tempCooling(i) = 2;
elseif CoolingC >= 2.5 && CoolingC <7.5
tempCooling(i) = 5;
elseif CoolingC >=7.5
tempCooling(i) = 10;
end
It could be solved like this, but is there a easy way to round to nearest number in a fixed vector?
RoundToNearest(CoolingCapacity,CoolingC) And it returns the rounded vector?
Im not sure if my question is clear. Else be free to ask me and ill answer.

Akzeptierte Antwort

Youssef  Khmou
Youssef Khmou am 19 Jan. 2015
Try logical test :
CoolingC = [1,2.3,5.3,7.2,10.2,12.2];
CoolingC(CoolingC<2.5)=2
CoolingC(CoolingC>=2.5 & CoolingC<7.5 )=5
CoolingC(CoolingC>=7.5 )=10
  1 Kommentar
Kelvin
Kelvin am 19 Jan. 2015
Thats exactly what im looking for. Logical test is a great solution and easy to read.
Thanks :-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by