How to transform data to have new minimum, maximum and average values?
3 views (last 30 days)
I have the following dataset with min = -6.3, max = 1.0 and mean = -3.3. Is there an easy way in matlab to transform these data to have a new minimum (-8.0), maximum (1.5) and mean (-2.9)? Thanks!
Nadia Shaik on 11 Oct 2022
I understand that you want to transform existing data to have new minimum, maximum and mean values.
The minimum, maximum and mean values can be altered in an iterative way.
The following code snippet illustrates how to alter the parameters.
new_mean = 6;
new_min = -2;
new_max = 9;
error = 0.01;
x= [-5.0 -6.3 -4.6 -2.4 0.3 1.0 -4.7 -4.9];
while abs(new_mean - mean(x)) >= error
if new_mean > mean(x)
x(find(x < new_mean,1)) = randi([new_mean new_max]);
elseif new_mean < mean(x)
x(find(x > new_mean,1)) = randi([new_min new_mean]);
The above code will work accurately for large array sizes.
I hope this helps.
dpb on 13 Oct 2022
OK, this has been rumbling around in back of head since posted -- one way that keeps the original dataset and adjusts them within the set bounds by tweaking each intermediate value after scaling...it's bizarre request, but does satisfy that --
x=[-5.0 -6.3 -4.6 -2.4 0.3 1.0 -4.7 -4.9].'; % sample data
MIN=-8.0; MAX=1.5; MEAN=-2.9; % target values
% the engine
[x,ix]=sort(x); % you'll see why here in a minute... :)
y=(rescale(x,MIN,MAX)); % scale first to min/max
N=numel(x); M=(N+1)/2; % median
dy=interp1([1 M N],[0 1 0],[1:N].'); % adjustment intermediate elements fixed endpoints
dx=fsolve(@(x)abs(fnY(x))-abs(MEAN),0); % find the needed adjustment to hit new mean
y=y+dx*dy; % the new vector values
y=y(ix); % and back in original order before adjustment
% show results
disp([min(y) max(y) mean(y)])
histfit(x), hold on, histfit(y)
subplot(3,1,2), hist(x), xlim([-8 2])
subplot(3,1,3), hist(y), xlim([-8 2])