How to Efficiently randomize N percent index in every column?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Dear Coder,
The objective was to mask a signal with different RANDOM noise combination. For example, if we have a signal
ori_signal=[1 4 7 5 1 4 4 6 2 8]';
we masked 30% of the signal with noise.
For simplicity, we let the first noise to be NaN while the second and third noise as a random value. The random value can be any value from the range of 1-9.
The picture below show the final output (e.g., P1_Final, P2_Final, P3_Final) after the original signal being masked with the 3 sets of random signal noise.
To realize the objective, the following code were constructed. However, I wonder if the is more simple or compact ways of realizing the same task?
ori_signal=[1 4 7 5 1 4 4 6 2 8]';
nRandCol=10;
data_rand=zeros(size(ori_signal,1),nRandCol);
ShufleNperc=3; % Randomize 30 percent of the data. where, 10% as NaN and 20% randomize its value (the value can be from 1 to 9)
possbleVal=[1 9]; % the value can be from 1 to 9 for the 20% randomize value
for f_x=1:nRandCol
data_change=ori_signal;
N = numel(data_change);
data_change(randperm(N,floor(ShufleNperc))) = NaN;
data_rand(:,f_x)=data_change;
end
data_rand_rs=data_rand(:);
[row, ~] = find(isnan(data_rand(:)));
row_loc= row(~ismember(row,row(1:3:end,:))); % The index of the second and third NaN in every column of data_rand
randm_no=randi(possbleVal,size(row_loc,1),1); % randomly choose 10
% randm_no=zeros(size(row_loc,1),1); Use zero for easy trouble shooting
data_rand_rs(row_loc)=randm_no;
noise_signal=reshape(data_rand_rs,size(data_rand,1),[]);
0 Commenti
Risposte (1)
KSSV
il 4 Dic 2017
N = 10 ;
signal = randi(9,N,1) ;
%%Add NaN's
P = 30 ; % percentage of numbers
idx = randperm(N,round(100/P)) ;
signal(idx) = NaN ;
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!