Filling up zeros forward/backward columnwise

2 visualizaciones (últimos 30 días)
Mohammad Sayeed
Mohammad Sayeed el 20 de En. de 2014
Comentada: Jos (10584) el 23 de En. de 2014
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
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
Mohammad Sayeed el 20 de En. de 2014
Yes it is possible to have two 0 in a row, but the zeros must be filled up by the previous value in the column if it is not the first value of the day. If is it the first value then it must be filled up with a subsequent data which has a non-zero value.

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
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
Image Analyst
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
Mohammad Sayeed el 23 de En. de 2014
Thank you again and sorry for the misunderstanding. I think my problem is more complex than what your codes will solve. May be I should have posted a sample dataset to make it clear. However, I will get some good insights from your codes to solve my problem. Thank you again.

Iniciar sesión para comentar.


Jos (10584)
Jos (10584) el 20 de En. de 2014
  3 comentarios
Mohammad Sayeed
Mohammad Sayeed el 23 de En. de 2014
Hi Jos, FILLZERO is not working in my MATLAB software. does it require any toolbox?
Jos (10584)
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:
  1. Download the file from the File Exchange (and unzip it)
  2. 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).
  3. Add this folder to the matlab path

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by