How can I manipulate my data? (Maybe with flagging)
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a matrix with data, it has a lot of rows and 7 columns. Later I want to devide the last three columns in blocks of 30, but not yet. In the second row is numerically listed in which month the data was recorded, and if the first day of one of these blocks falls in one of the months from april til september (month 4 - 9) I want to remove the entire block.
So basically I want to look for every block of 30 if the first entry of the second column is 4-9 and remove this row and the 29 rows that follow if so. Maybe something with flagging?
댓글 수: 0
답변 (1개)
Image Analyst
2014년 5월 3일
Why not just remove every row where column 6 is in the range 4-9 inclusive? To do that
rowsToDelete = array(:, 6) >= 4 & array(:, 6) <= 9;
array(rowsToDelete, :) = [];
댓글 수: 2
Image Analyst
2014년 5월 3일
편집: Image Analyst
2014년 5월 3일
Then try a for loop
%array = randi(12, 2000, 7); % Sample data
[rows, columns] = size(array)
rowsToDelete = false(rows, 1);
for row = 1 : length(rowsToDelete)
deleteThisRow = array(row, 6) >= 4 & array(row, 6) <= 9;
if deleteThisRow && row == 1
% Special case of first row being in range 4-9.
rowsToDelete(1:30) = true;
elseif deleteThisRow && (array(row - 1, 6) < 4 || array(row - 1, 6) > 9)
% This row in range 4-9 but prior row is not.
% This means it is the start of a run of 4-6 of some length.
% Flag next 30 rows for deletion.
lastIndex = min([rows, row+29]); % Don't go past end of array!
rowsToDelete(row:lastIndex) = true;
end
end
% Warnings : If some run of 4-6 is less than 30, say 28 or 29,
% it will still delete the next 30 rows.
% If the run is more than 30, say 31 like in July and August,
% It will leave the last day (the 31st) in the array.
array(rowsToDelete, :) = [];
[rows, columns] = size(array) % See what we ended up with.
You didn't attach any data so I can't really try it. Attach your data file and code to read it if you want. This method is less robust and more complicated than my first suggestion for reasons given in the warnings at the end of the code, but I believe it will do what you asked for (even if that's not right). The "rowsToDelete" array is the "flag" array you were referring to.
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!