# i need to convert below batrix to another matrix

조회 수: 1(최근 30일)
b=[308 -1 2 3 -1 2 -1;
-1 -1 2 1 0 1 0;
1 2 -1 -1 -1 -1 2;
0 0 1 -1 -1 2 -1];
to
b=[308 2 3 2 ;
2 1 0 1 0;
1 2 2;
0 0 1 2 ]
fill all ending elements with 0
b=[308 2 3 2 0 ;
2 1 0 1 0;
1 2 2 0 0;
0 0 1 2 0]
thanks

#### 댓글 수: 1

Walter Roberson 8 Nov 2019
Is it correct that the rule is that each row should have all negative values removed, and then all rows should be padded out with 0 to the length of the longest remaining row ?

로그인 to comment.

### 채택된 답변

KSSV 8 Nov 2019
b=[308 -1 2 3 -1 2 -1;
-1 -1 2 1 0 1 0;
1 2 -1 -1 -1 -1 2;
0 0 1 -1 -1 2 -1];
% Get positive count from each row
N = sum(b>0,2) ;
iwant = zeros(size(b,1),max(N)) ;
for i = 1:size(b,1)
bi = b(i,:) ;
iwant(i,1:N(i)) = bi(bi>0) ;
for

#### 댓글 수: 1

Walter Roberson 8 Nov 2019
Should be b>=0 as row 2 shows an example of a 0 being preserved.

로그인 to comment.

Nitin Sapre 8 Nov 2019
Yes sir that's what I need

#### 댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

R2018a

Translated by