フィルターのクリア

How to do element by element comparison?

2 ビュー (過去 30 日間)
Rob
Rob 2012 年 10 月 17 日
Given a 2d matrix, say 10x4 with many numbers, lots of which are zeros, I need a matrix that replaces every zero entry with the value of the last non-zero entry in the same column that is before it. for example for the given input:
[0 0 4 0;
0 3 1 0;
0 0 0 2;
0 0 6 2;
3 4 5 6;
8 0 0 9;
0 0 0 0;
0 0 0 0;
0 0 0 0]
output:
[0 0 4 0;
0 3 1 0;
0 3 1 2;
0 3 6 2;
3 4 5 6;
8 4 5 9;
8 4 5 9;
8 4 5 9;
8 4 5 9]
I've experimented with circshift, if statements and for loops but haven't been able to make much headway. Does anyone have suggestions?
  1 件のコメント
Rob
Rob 2012 年 10 月 17 日
Brilliant!! Thank you!!

サインインしてコメントする。

採用された回答

venkat vasu
venkat vasu 2012 年 10 月 17 日
編集済み: Matt Fig 2012 年 10 月 17 日
Hi.. This code surely will help you and you can check n matrix also.
a=[0 0 4 0; 0 3 1 0; 0 0 0 2; 0 0 6 2; 3 4 5 6; 8 0 0 9; 0 0 0 0; 0 0 0 0; 0 0 0 0];
[r c d]=size(a);
for i=1:r
j=a(i,:);
if i==1
prev=j;
else
b=find(j==0);
for k=1:length(b);
a(i,b(k))=prev(b(k));
end
prev=a(i,:);
end
end
a

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by