Delete rows from a table below a certain threshold

129 visualizaciones (últimos 30 días)
Cathal
Cathal el 3 de Ag. de 2023
Comentada: Jon el 3 de Ag. de 2023
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
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.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
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)
Table = 10×2
1.0000 0.0755 2.0000 0.8808 3.0000 0.9778 4.0000 0.0265 5.0000 0.8948 6.0000 0.1272 7.0000 0.8180 8.0000 0.8969 9.0000 0.8362 10.0000 0.7097
Threshold_Number = T;
rowsToDelete = Table(:,2) < T;
Table(rowsToDelete,:) = []
Table = 7×2
2.0000 0.8808 3.0000 0.9778 5.0000 0.8948 7.0000 0.8180 8.0000 0.8969 9.0000 0.8362 10.0000 0.7097
  3 comentarios
Voss
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
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!

Iniciar sesión para comentar.

Más respuestas (1)

Jon
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)
Table = 10×2
1.0000 -6.2349 2.0000 -17.9289 3.0000 6.4076 4.0000 -3.3890 5.0000 3.9484 6.0000 1.0207 7.0000 17.3471 8.0000 14.6419 9.0000 0.5553 10.0000 15.1793
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,:)
Table = 4×2
2.0000 -17.9289 7.0000 17.3471 8.0000 14.6419 10.0000 15.1793
Here I have followed your code, but I would suggest not calling your variable "Table", when in fact it is an array.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by