필터 지우기
필터 지우기

how to change a number randomly???...

조회 수: 1 (최근 30일)
sani ars
sani ars 2012년 11월 30일
I have a column matrix that contains class labels in it. It has 35 rows... There are 4 class labels ( 1 2 3 4 )...
labels = [1 1 3 2 4 1 2 4 3 3 2 1 4 2 3 4 4 2 2 1 3 2 4 4 2 3 1 1 2 4 3 1 2 1 3] '
now I randomly select 5% of data from it as
N_of_labels= size(labels,1)
prcnt_labels = ceil(N_of_labels*5/100);
rng(1,'twister');
rand_class_index = randsample(N_of_labels,prcnt_labels);
labels(rand_class_index) = rem(labels(rand_class_index),4)+1;
the purpose of it to make the class labels noisy (changing the actual label).... Now here, rem() will always change the label 1 into 2 and 2 into 3 and so on... I need to change the labels randomly i.e. I want that 2 should be change to any number from 1 3 4 but not always to 3.... how can I do that???...

채택된 답변

Jan
Jan 2012년 11월 30일
편집: Jan 2012년 11월 30일
What about adding random values between 0 and 3 before REM:
rem(labels(rand_class_index) + randi([0, 3], prcnt_labels, 1), 4) + 1
If the label must be changed, use the range [1,3].
[EDITED after your comment]
randi([0,3]) replies number from 0 to 3. Then a value can be unchanged. If a change is obligatory, use the range [1,3] as mentioned already:
rem(labels(rand_class_index) + randi([1, 3], prcnt_labels, 1), 4) + 1
  댓글 수: 2
sani ars
sani ars 2012년 11월 30일
I have tried it... just change it as:
rem(labels(rand_class_index) + randi([1,4], prcnt_labels, 1),4)+1
its working fine..... But at some places it is not changing the label e.g. if the label is 4 it still remain 4 as the randi () is randomly giving the number..
For this, I tried to match the actual labels (of selected indices) with the changed one as:
tr = zeros(size(rand_class_index,1),2);
tr = rem(labels(rand_class_index) + randi([1,4], prcnt_labels, 1),4)+1
labl_val = labels(rand_class_index)
for i = 1:size(rand_class_index,1)
if tr(i,1)== labl_val(i,1)
tr(i,1) = labl_val(i,1)+1;
end
end
labels(rand_class_index) = tr(:,1)
// I need to rewrite following line of code
tr(i,1) = labl_val(i,1)+1;
because using this line, if the label (labl_val) is 4 it will become 5 and I don't want 5.. I want that it should be change to any number from 1 2 3..
Just need to correct this line... Can someone plz tell about it??..
Jos (10584)
Jos (10584) 2012년 11월 30일
if tr(i,1) == 5, tr(i,1) = 1 ; end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Fig
Matt Fig 2012년 11월 30일
labels(randperm(numel(labels)))
  댓글 수: 1
sani ars
sani ars 2012년 11월 30일
I just want to change the values of selected indexes, not the whole labels

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by