How to Fill matrix rows with another row?
4 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I want fill the zero containing rows of this matrix with the row on top of it. For example, row (2:9) should be a copy of row one. Row (11:18), copy of row ten so on and so forth. This algorithm should be implemented till the end of (3240x9) matrix which is in the attachment.
1.843 6.97 -0.1 -0.001420 1.556600 -7.39140 2.1620 -3.3124 2.084
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1.835 7.0751 -5.86 0.179 -0.0030 4.378 -3.37 1.476 -2.81
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 个评论
采纳的回答
KSSV
2021-6-27
Let A be your matrix.
[m,n] = size(A) ;
for i = 1:m
if ~any(A(i,:))
A(i,:) = A(i-1,:) ;
end
end
0 个评论
更多回答(1 个)
Fawad Farooq Ashraf
2021-6-27
Well you could find the indices of all the nonzero rows for a matrix A as
idx = find(~all(A==0,2));
and then you can replicate rows from idx(1)+1:idx(2)-1 equal to A(idx(1),:) using repmat function in matlab.
There could be a better logic for this but right now the one that's coming to my mind is something like this,
for i = 1:length(idx)-1
A(idx(i)+1:idx(i+1)-1,:) = repmat(A(idx(i),:),length(idx(i)+1:idx(i+1)-1),1);
end
A(idx(end)+1:size(A,1),:) = repmat(A(idx(end),:),length(idx(end)+1:size(A,1)),1);
You can develop your own logic but this kind of works as well.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!