필터 지우기
필터 지우기

How do I get the maximum numbers of every "increasing pattern" in each row in a matrix?

조회 수: 2 (최근 30일)
Here is a matrix A consisting of "increasing patterns".
A=[1,2,3,1,2,3,4,1,2,1,2,3,4,1,1;1,2,1,1,2,3,1,2,3,4,1,2,3,4,5]
In each row of matrix A, all "increasing patterns" begin with 1.
Visually, all "patterns" are underlined separately below. The largest numbers in all "patterns" are in bold.
A =
1 2 3 1 2 3 4 1 2 1 2 3 4 1 1
1 2 1 1 2 3 1 2 3 4 1 2 3 4 5
How do I get B? This is, get the maximum numbers of all "patterns" in each row.
B=3 4 2 4 1 1
2 1 3 4 5 0
% use zero to fuilfill matrix B to make dimensions of each row consistent

채택된 답변

Stephen23
Stephen23 2018년 12월 27일
>> A = [1,2,3,1,2,3,4,1,2,1,2,3,4,1,1;1,2,1,1,2,3,1,2,3,4,1,2,3,4,5]
A =
1 2 3 1 2 3 4 1 2 1 2 3 4 1 1
1 2 1 1 2 3 1 2 3 4 1 2 3 4 5
>> X = diff(A,1,2)~=1;
>> X(:,end+1) = true;
>> B = A.*X
B =
0 0 3 0 0 0 4 0 2 0 0 0 4 1 1
0 2 1 0 0 3 0 0 0 4 0 0 0 0 5
  댓글 수: 3
Stephen23
Stephen23 2018년 12월 27일
편집: Stephen23 2018년 12월 27일
>> Y = B.';
>> Z = +~sort(Y==0,1);
>> Z(Z==1) = Y(Y~=0);
>> B = Z(any(Z,2),:).'
B =
3 4 2 4 1 1
2 1 3 4 5 0

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

추가 답변 (1개)

Linjun He
Linjun He 2018년 12월 27일
편집: Linjun He 2018년 12월 27일
A2=[A,ones(size(A,1),1)]-[ones(size(A,1),1),A]<=0
B=A2(:,2:end).*A
Here is what I get.
B =
0 0 3 0 0 0 4 0 2 0 0 0 4 1 1
0 2 1 0 0 3 0 0 0 4 0 0 0 0 5
It's not a good answer but it's ok for me.
I am still looking forward to a better answer.
  댓글 수: 2
madhan ravi
madhan ravi 2018년 12월 27일
but in the orginal question your desired output is different right I mean 5 was padded with 0 at the end??
Linjun He
Linjun He 2018년 12월 27일
It's not a good answer but it's ok for me.
I am still looking forward to a better answer.

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

카테고리

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