How to Fill matrix rows with another row?

11 ビュー (過去 30 日間)
Mohammad Shafi Nikzada
Mohammad Shafi Nikzada 2021 年 6 月 26 日
回答済み: Fawad Farooq Ashraf 2021 年 6 月 27 日
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

採用された回答

KSSV
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

その他の回答 (1 件)

Fawad Farooq Ashraf
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.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by