fill in zeros in a matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all, I have a 1000x1 double matrix. It is full of numbers....but on some situations I get multiple 0s.
Whenever I encounter 0's, I want to fill the zeros with the average of the two numbers (the nearest non zero numbers above and below).
For example, if we had 3, 4, 0 ,3 , 2, 2, 0 ,0, 0, 3, 2, 1
we would get 3, 4, 3.5, 3, 2, 2, 2.5, 2.5, 2.5, 3, 2, 1.
The shorter and faster the code, the better....please remember the amount of consecutive zeros vary.
0 Kommentare
Akzeptierte Antwort
Jan
am 10 Jul. 2013
x = [3, 4, 0 ,3 , 2, 2, 0 ,0, 0, 3, 2, 1];
m = (x == 0);
x(m) = interp1(find(~m), x(~m), find(m));
0 Kommentare
Weitere Antworten (1)
Matt J
am 9 Jul. 2013
I would recommend replacing the zeros with NaNs and using something like this,
instead.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!