Find zeros and remove
2 次查看(过去 30 天)
显示 更早的评论
Hi, I have a wide excel file with several columns. I need to "clean" it from meaningless values, and obtain matrix with only three columns with the values I need to analyse. Here is an example of how the data look like in the excel file (numbers are invented):
Xdat X Y
0 0 0
0 0 0
0 0 0
0 12 12
8 2 2
8 2 2
8 2 2
8 2 2
8 2 2
8 2 2
0 16 24
0 16 24
0 0 0
0 10 10
0 0 0
0 0 0
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
0 16 24
0 16 24
0 0 0
0 10 10
0 0 0
0 0 0
8 5 5
8 5 5
8 5 5
8 5 5
8 5 5
8 5 5
The problem is simple: I need it to look through the Xdat column, find the zeros and removing the corresponding rows. At the end I should obtain a matrix like that:
8 2 2
8 2 2
8 2 2
8 2 2
8 2 2
8 2 2
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
8 5 5
8 5 5
8 5 5
8 5 5
8 5 5
8 5 5
Another thing I could say is that Xdat values go from 1 to 72, but are in random order and repeated twice. So there are in total 144 Xdat values (two ones, two twos, two threes and so on). I therefore need to be sure that columns and rows with the same Xdat value are not overwritten. (As shown above).
The only solution that comes to my mind is to eliminate all rows corresponding to the 0 Xdat.
Hope I've been clear. If you need further explanation please let me know. Thank you
Giulia
0 个评论
回答(2 个)
Azzi Abdelmalek
2014-6-14
编辑:Azzi Abdelmalek
2014-6-14
Suppose M is your matrix
M=[1 1 10
0 0 0
4 0 0
0 12 12
8 2 2
0 2 2]
M(M(:,1)==0,:)=[]
0 个评论
Image Analyst
2014-6-14
Try this:
%numbers = xlsread(filename); % Uncomment to read workbook.
numbers = [... % Remove this if you read in a workbook.
0 0 0
0 0 0
0 0 0
0 12 12
8 2 2
8 2 2
8 2 2
8 2 2
8 2 2
8 2 2
0 16 24
0 16 24
0 0 0
0 10 10
0 0 0
0 0 0
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
1 3 3
0 16 24
0 16 24
0 0 0
0 10 10
0 0 0
0 0 0
8 5 5
8 5 5
8 5 5
8 5 5
8 5 5
8 5 5]
% MAIN CODE IS BELOW:
% Find rows to delete:
rowsToExtract = all(numbers, 2)
% Delete in place to change existing array:
numbers = numbers(rowsToExtract,:)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!