필터 지우기
필터 지우기

How can i replace a number with its no of ocurrences

조회 수: 2 (최근 30일)
Ayesha Maroof
Ayesha Maroof 2015년 5월 28일
편집: Mohammad Abouali 2015년 5월 28일
i have a matrix=a=[1 0 1 0 1;0 1 1 1 1;1 1 0 1 1;1 1 1 1 1] i want to replace ones by their no of occurences in each row like a=[1 0 2 0 3;0 1 2 3 4;1 2 0 3 4;1 2 3 4 5]

채택된 답변

Mohammad Abouali
Mohammad Abouali 2015년 5월 28일
편집: Mohammad Abouali 2015년 5월 28일
a=[1 0 1 0 1;0 1 1 1 1;1 1 0 1 1;1 1 1 1 1];
cumsum(a,2).*a
ans =
1 0 2 0 3
0 1 2 3 4
1 2 0 3 4
1 2 3 4 5
  댓글 수: 2
Ayesha Maroof
Ayesha Maroof 2015년 5월 28일
thanx alot Mohammad Abouali.i lilttle more help needed.can i shuffle the values now.that zeros remain and its place but rest values shuffles with in a row like [3 0 1 0 2;0 2 1 4 3...] like this
Mohammad Abouali
Mohammad Abouali 2015년 5월 28일
편집: Mohammad Abouali 2015년 5월 28일
a=[1 0 1 0 1;0 1 1 1 1;1 1 0 1 1;1 1 1 1 1];
b=cumsum(a,2).*a;
disp('b:')
disp(b)
will print:
b:
1 0 2 0 3
0 1 2 3 4
1 2 0 3 4
1 2 3 4 5
Now shuffling:
shuffle=@(data) data(randperm(numel(data)));
for row=1:size(a,1)
mask=logical(a(row,:));
b(row,mask)= shuffle(b(row,mask));
end
disp('shuffled b:')
disp(b)
will print: (well it is random, so each time prints something different)
shuffled b:
2 0 3 0 1
0 1 3 4 2
1 3 0 2 4
4 5 3 1 2

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by