Delete rows from a table below a certain threshold
56 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Dyuman Joshi
el 3 de Ag. de 2023
Editada: Dyuman Joshi
el 3 de Ag. de 2023
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.
Respuesta aceptada
Voss
el 3 de Ag. de 2023
% 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 comentarios
Voss
el 3 de Ag. de 2023
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
el 3 de Ag. de 2023
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!
Más respuestas (1)
Jon
el 3 de Ag. de 2023
Editada: Jon
el 3 de Ag. de 2023
% 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 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!