Filling up zeros forward/backward columnwise
5 views (last 30 days)
Show older comments
Hi I need to fill up zeros by the previous data columnwise in a matrix containing 5 minutes data for 10 days. each day I have 288 data so total number of rows is 288*10=2880 and number of column if say, 5. if the first data of a given day is zero it will filled up with the next data. Can anybody help by proving MATLAB codes for doing this?
2 Comments
Walter Roberson
on 20 Jan 2014
Is it possible for there to be two 0 in a row? Is it possible for a row to be all 0 ?
Answers (2)
Image Analyst
on 20 Jan 2014
Edited: Image Analyst
on 20 Jan 2014
Something like this maybe (untested)
[rows, columns] = size(m);
for row = 1 : rows
for col = 2 : columns
if m(row, col) == 0
m(row, col) = m(row, col-1);
end
end
end
3 Comments
Image Analyst
on 20 Jan 2014
You said columnwise so I thought you wanted to go across columns first, in a given row, and then move down to the next row once all the columns in the active row have been processed. I think column-wise means columns first, then rows. I guess you and I have different definitions of column-wise. So you want to go down rows first, and then move over to the next column once all the rows in that column have been processed. Try this (again, untested):
[rows, columns] = size(m);
for col = 1 : columns
% Fill up zeros if column starts with zero.
if m(1, col) == 0
% First row of this column is zero.
firstNonZeroRow = find(m(:, col)~= 0, 1, 'first');
% Make all prior rows have that value.
valueToUse = m(firstNonZeroRow, col); % Get the value.
m(1:firstNonZeroRow-1, col) = valueToUse;
end
for row = 2 : rows
if m(row, col) == 0
m(row, col) = m(row, col-1);
end
end
end
Jos (10584)
on 20 Jan 2014
This looks like a job for FILLZERO :
3 Comments
Jos (10584)
on 23 Jan 2014
Not working is too vague … What is the problem? Note that you need to perform these steps for a File Exchange file to work on your system:
- Download the file from the File Exchange (and unzip it)
- Copy the m-file to a specific folder (e.g. "Useful_mfiles") somewhere in your user or programming folder (not in a systems or matlab folder).
- Add this folder to the matlab path
See Also
Categories
Find more on Database Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!