필터 지우기
필터 지우기

How to replace duplicate element to 0 in matrix for every rows

조회 수: 2 (최근 30일)
ahmad
ahmad 2022년 5월 24일
답변: KSSV 2022년 5월 24일
I need to replace the repeated elements in column of a matrix as 0's, If my matrix is like this means.
Input =
1 1 1 2 2 2 3 3 4 4 5 5 5
1 2 2 3 3 3 4 4 4 5 5 6 6
1 1 1 1 2 2 3 4 5 5 5 6 6
My expected output should be like this
Output =
1 0 0 2 0 0 3 0 4 0 5 0 0
1 2 0 3 0 0 4 0 0 5 0 6 0
1 0 0 0 2 0 3 4 5 0 0 6 0

채택된 답변

KSSV
KSSV 2022년 5월 24일
A = [1 1 1 2 2 2 3 3 4 4 5 5 5
1 2 2 3 3 3 4 4 4 5 5 6 6
1 1 1 1 2 2 3 4 5 5 5 6 6] ;
B = zeros(size(A)) ;
for i = 1:size(A,1)
[c,ia,ib] = unique(A(i,:)) ;
B(i,ia) = c ;
end
B
B = 3×13
1 0 0 2 0 0 3 0 4 0 5 0 0 1 2 0 3 0 0 4 0 0 5 0 6 0 1 0 0 0 2 0 3 4 5 0 0 6 0

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by