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
Walter Roberson 2019-11-8
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
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 个)

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

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by