Filtering data from a table. How can I save values that are higher that 0.5?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Turlough Hughes
2019-12-7
It would be helpful to attach your variable as ".mat", also are you looking to save all rows of a based on a(:,4)?
回答(3 个)
Pandiyaraj Gnanasekar
2019-12-7
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
2019-12-7
编辑:Turlough Hughes
2019-12-7
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 个评论
Turlough Hughes
2019-12-8
"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
2019-12-7
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!