Filtering data from a table. How can I save values that are higher that 0.5?
1 view (last 30 days)
Show older comments
I can't find a solution to my problem. I have data stored in a table. This data represent the behavior of a signal in time.
I want to create a program that reads each row of a table if a value on a specific column is 0 do not save, but if the value is higher start saving.
Example:
0 0 0 0 0 1 1.2 1.5 1.6 1.3 1 0 0 0 0 0 0 0 0 2 2.4 5 3 2 0 0 0 0 0 0 0 0 0 0 . . . . . . . . . .
--------------- save 1 ------------------- save 2 -------------------------
Code:
b=a(:,4) %column to search
rate=length(b) %number of rows
for i=1:rate
b1 = b(i); % read value of row
if b1 < 0.5
end
if b1 > 0.5
end
end
*I have an overwrite problem
Hope you can help me. Thanks.
2 Comments
Turlough Hughes
on 7 Dec 2019
It would be helpful to attach your variable as ".mat", also are you looking to save all rows of a based on a(:,4)?
Answers (3)
Pandiyaraj Gnanasekar
on 7 Dec 2019
Hi Mariana Gutierrez,
b = a ( : , 4 ); %column to search
rate = length(b); %number of rows
V = zeros(rate,1);
for i =1:rate
if b(i) > 0.5
V(i) = b(i);
end
end
I think this might help! Try this or explain a bit more in next comment.
Turlough Hughes
on 7 Dec 2019
Edited: Turlough Hughes
on 7 Dec 2019
You could get an index for each chunk of data as follows:
idx=find(a(:,4)>0.5)
idx2=find((idx(2:end)-idx(1:end-1))~=1);
idx2=unique(sort([min(idx); idx(idx2); idx(idx2+1); max(idx)])); % this is the start and end of each chunk of data.
Then to store data like this you would typically put it into a cell array:
for c=1:length(idx2)/2
data{c,1}=a(idx2(2*c-1):idx2(2*c),:);
end
EDIT: As per comments below
6 Comments
Turlough Hughes
on 8 Dec 2019
"I tried it , but is not working it also saves 0 values"
I subsequently edited my answer. There was a problem with the indexing but it should be fine now.
Pandiyaraj Gnanasekar
on 7 Dec 2019
Hi Mariana Gutierrez,
If you want only the values greater than 0.5 and not the zeros. You can simply remove those zeros from the vector b.
try this,
b = a ( : , 4 );
b(b==0) = []; % this will remove values equal to zeros from b vector.
Hope it will solve your problem.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!