Filling up zeros forward/backward columnwise

조회 수: 7 (최근 30일)
Mohammad Sayeed
Mohammad Sayeed 2014년 1월 20일
댓글: Jos (10584) 2014년 1월 23일
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
Walter Roberson
Walter Roberson 2014년 1월 20일
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 2014년 1월 20일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2014년 1월 20일
편집: Image Analyst 2014년 1월 20일
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
Image Analyst
Image Analyst 2014년 1월 20일
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 2014년 1월 23일
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.

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 2014년 1월 20일
  댓글 수: 3
Mohammad Sayeed
Mohammad Sayeed 2014년 1월 23일
Hi Jos, FILLZERO is not working in my MATLAB software. does it require any toolbox?
Jos (10584)
Jos (10584) 2014년 1월 23일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by