Delete rows from a table below a certain threshold
86 次查看(过去 30 天)
显示 更早的评论
I have a double table with two columns, Time and Data. I want to delete values out of both columns in the table if it is below a certain value in the 'Data' column. The problem I am encountering is that the data column conatins large values and the time column contains small values so the code I'm using deletes the values from 'Data', working as intended, but then also wipes the entirety of the Time column as all the values are smaller than the threshold. How can I modfiy this to delete Time rows corresponding to the deleted Data rows and leave the rest?
Table = cat(2,Time,Data)
Threshold_Number = T
rowsToDelete = Table < T;
Table(rowsToDelete) = [];
1 个评论
Dyuman Joshi
2023-8-3
编辑:Dyuman Joshi
2023-8-3
Numeric data variables are not called Tables in context of MATLAB.
If you have to compare the values in Data column to check, then why are you comparing the threshold with the whole array (3rd line of code above)?
Compare the first row, and use output as the row indices.
采纳的回答
Voss
2023-8-3
% Example Time, Data and T:
Time = (1:10).';
Data = rand(10,1);
T = 0.5;
% Your code, modified:
Table = cat(2,Time,Data)
Threshold_Number = T;
rowsToDelete = Table(:,2) < T;
Table(rowsToDelete,:) = []
3 个评论
Voss
2023-8-3
Not that I know of.
I added my answer mostly to show the OP that their approach (deleting the rows) would work fine once the indexing was done correctly.
In my opinion, in general it's best to change as little as necessary from the OP's original approach - change only whatever is necessary to get the code to work. A beginner may not know what changes were relevant to the problem at hand and which were more just stylistic choices.
Jon
2023-8-3
Good point. I hadn't noticed that the original post also used the assignment to empty matrix. In the future, I'll try to pay more attention to staying as close to OP's posts as possible, and then if there is a better way, suggest that as an alternative. -Thanks!
更多回答(1 个)
Jon
2023-8-3
编辑:Jon
2023-8-3
% Make up some example data
Time = [1:10]';
Data = randn(10,1)*10;
Table = cat(2,Time,Data)
Threshold = 8;
% Delete rows where Data is less than threshold
% (actually we are keeping only rows where Data is bigger than threshold)
Table = Table(abs(Data)>Threshold,:)
Here I have followed your code, but I would suggest not calling your variable "Table", when in fact it is an array.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!