Filling up zeros forward/backward columnwise
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 20 de En. de 2014
Is it possible for there to be two 0 in a row? Is it possible for a row to be all 0 ?
Mohammad Sayeed
el 20 de En. de 2014
Respuestas (2)
Image Analyst
el 20 de En. de 2014
Editada: Image Analyst
el 20 de En. de 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 comentarios
Mohammad Sayeed
el 20 de En. de 2014
Image Analyst
el 20 de En. de 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
Mohammad Sayeed
el 23 de En. de 2014
Jos (10584)
el 20 de En. de 2014
0 votos
This looks like a job for FILLZERO :
3 comentarios
Jos (10584)
el 20 de En. de 2014
and if you want it backward, use FLIPUD before and after.
Mohammad Sayeed
el 23 de En. de 2014
Jos (10584)
el 23 de En. de 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
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!