Delete rows from a table below a certain threshold

조회 수: 35 (최근 30일)
Cathal
Cathal 2023년 8월 3일
댓글: Jon 2023년 8월 3일
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
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
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)
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
Voss
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
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
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)
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by