How to set bottom repeating elements in matrix to NaN?

조회 수: 2 (최근 30일)
Sushil Pokharel
Sushil Pokharel 2022년 6월 16일
이동: Dyuman Joshi 2023년 8월 26일
I have matrix of large size and I need to change the bottom repeating elements to nan. For instance:
a = [ 1 2 3 2 1 3
3 1 1 3 1 2
1 2 3 3 2 1
1 2 1 2 1 3
1 2 3 1 2 1];
In this matrix, I want to change the bottom repeating numbers. If the numbers are repeating on top, I don't want to do anything to them. Just want to replace the bottom repeating number by NaNs. Any help will be greatly appreciated.
Thank you.
  댓글 수: 7
Dyuman Joshi
Dyuman Joshi 2022년 6월 16일
이동: Dyuman Joshi 2023년 8월 26일
Okay. Just a note - If you want a general answer, do mention information explicitly. Answers will always be tailored according to the information you give. So limited data results in particular answer rather than a general solution.
I could only understand what you want to obtain after you gave another example above. Nonetheless, you got your answer.
Sushil Pokharel
Sushil Pokharel 2022년 6월 16일
이동: Dyuman Joshi 2023년 8월 26일
@Dyuman Joshi you are absolutely right. Sorry for the inconvenience.

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

채택된 답변

Voss
Voss 2022년 6월 16일
a = [ 1 2 3 2 1 3
3 1 1 3 1 2
1 2 3 3 2 1
1 2 1 2 1 3
1 2 3 1 2 1];
to_nan = cummin(a(1:end-1,:) == a(end,:),1,'reverse');
to_nan = to_nan([1:end end],:);
a(to_nan) = NaN
a = 5×6
1 2 3 2 1 3 3 1 1 3 1 2 NaN NaN 3 3 2 1 NaN NaN 1 2 1 3 NaN NaN 3 1 2 1
  댓글 수: 4
Sushil Pokharel
Sushil Pokharel 2022년 6월 17일
hi @Voss, I really appreciate your help. now I have clear understanding about the code. Thank you so much for your help.
Voss
Voss 2022년 6월 17일
You're welcome!

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by