i need to convert below batrix to another matrix

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

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 ?

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

 채택된 답변

KSSV
KSSV 2019년 11월 8일

0 개 추천

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

Should be b>=0 as row 2 shows an example of a 0 being preserved.

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

추가 답변 (1개)

Nitin Sapre
Nitin Sapre 2019년 11월 8일

0 개 추천

Yes sir that's what I need

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2019년 11월 8일

댓글:

2019년 11월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by