필터 지우기
필터 지우기

Select and remove/replace values in matrix

조회 수: 2 (최근 30일)
JL
JL 2019년 7월 23일
댓글: JL 2019년 8월 8일
My question is to check each row for the same numbers (in this case 1 or 0, we leave the 0s unchanged in the matrix), and replace any 1 in the row with 0 except the last value of 1
Y = [0 1 1 0 1 0;
0 0 0 1 0 1;
1 0 1 0 1 0; ]
The end result I should get is
X = [0 0 0 0 1 0;
0 0 0 0 0 1;
0 0 0 0 1 0;]
Another similar question would be to remover the first/second (sometimes even third) number in the row leaving the last 2 numbers unchanged.
Y = [1 2 4 0 0 0;
2 5 0 0 0 0;
3 4 5 6 0 0;]
The end result I should get is
X = [0 2 4 0 0 0;
2 5 0 0 0 0;
0 0 5 6 0 0;]

채택된 답변

Guillaume
Guillaume 2019년 7월 23일
편집: Guillaume 2019년 7월 23일
One way:
nelems = 2; %number of elements to keep
X = (cumsum(Y>0, 2, 'reverse') < nelems+1) .* Y
  댓글 수: 6
JL
JL 2019년 7월 24일
Thank you, it is working!
JL
JL 2019년 8월 8일
Hi Guillaumen, I have another intetesting problem, was wondering if you are interested to take a look - here is the link to the question https://uk.mathworks.com/matlabcentral/answers/475460-sharing-values-of-elements-in-a-matrix

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by