lundi 26 avril 2021

How to make weighted random sampling for matrix - Matlab

I would like to create a weighted sample from an m by n matrix starting from an excel file ("DataTab", please see image 1). First column (UGT) represents the ID of the matrix and the column B-F represent the probability associated to the variable "fi" for each UGT. "fi" is a scalar that has value 10, 20, 30, 40, 50: this means that UGT 1101 has 50% of probability to have value 10 or 20, the UGT 1102 can be 30 or 40, so on...

Image 1

I used "randsample function" that works only for scalar, and i can not be able to use it for my situation.

fi = [10,20,30,40,50];
p = [0.15,0.20,0.30,0.10,0.25];
n=10000;
sample = randsample (fi, n, true, p);
hist(sample,fi);
bar(fi,n*p,0.7);

I put the entire code which works correcty with "Fv2" variable, not working with "Fv1" which is the goal of my question.

Nsample = 10000 
DataTab = xlsread('Scenari_stabilita_R6.xlsx','S1','A2:f6');
Ugt   = csvread('raster_ugt.acs'); 
UgtV   = reshape(Ugt,[],1);
MRas   = [UgtV];
MCycle  = MRas(~idxNaN,:);
a=unique(MCycle(:,4));
idxDataTab=ismember(DataTab(:,1),a);
DataTab2 = DataTab(idxDataTab,:);
nUGT = length(a);    
fi = [10,20,30,40,50];
Fv = [];
    for i = 1:nUGT
        Fv1 = randsample (fi, Nsample, true, DataTab2(i,:));
        %Fv2 = (DataTab2(i,3)-DataTab2(i,2)).*rand(Nsample,1) +
        %DataTab2(i,2); % this line calculates uniform distribution and
        %it has to be modified into weighted sampling
        Fv = [Fv,Fv1];
    end

The "Fv1" variable must be like this:

Image 2 - final risults

Anyone can help me, please?




Aucun commentaire:

Enregistrer un commentaire