Filling up zeros forward/backward columnwise
이전 댓글 표시
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
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
2014년 1월 20일
답변 (2개)
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
Mohammad Sayeed
2014년 1월 20일
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
2014년 1월 23일
Jos (10584)
2014년 1월 20일
0 개 추천
This looks like a job for FILLZERO :
댓글 수: 3
Jos (10584)
2014년 1월 20일
and if you want it backward, use FLIPUD before and after.
Mohammad Sayeed
2014년 1월 23일
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:
- 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
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!