Changing value when first time a value appears

조회 수: 3 (최근 30일)
Mia Dier
Mia Dier 2021년 1월 14일
댓글: Mia Dier 2021년 1월 14일
A=[1; 1; 1; 1; 2; 2; 4; 4; 4; 4]
B=[4; 2; 7; 9; 5; 0; 2; 6; 1; 0]
I want to change the values of B to NaN whenever a value in A appears for the first time. i.e.
B=[NaN; 2; 7; 9; NaN; 0; NaN; 6; 1; 0]

채택된 답변

Walter Roberson
Walter Roberson 2021년 1월 14일
A=[1; 1; 1; 1; 2; 2; 4; 4; 4; 4]
A = 10×1
1 1 1 1 2 2 4 4 4 4
B=[4; 2; 7; 9; 5; 0; 2; 6; 1; 0]
B = 10×1
4 2 7 9 5 0 2 6 1 0
[~, ia] = unique(A, 'stable')
ia = 3×1
1 5 7
B(ia) = nan
B = 10×1
NaN 2 7 9 NaN 0 NaN 6 1 0

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by