Do calculations for lines that are true
1 次查看(过去 30 天)
显示 更早的评论
How can I make matlab do a calculation, that will require a previous line, for a condition being true? Please reference the file I've attached.
When the 'z' vector is true, or 1, I want to take the current line's 'v', and subtract the previous line's 'v'. Then, if the next line is also true, to do the same calculation; so on and so forth until z = 0.
I will then want to take an average over each given interval of z being true, and put that into a new vector.
Any help is appreciated!
8 个评论
Guillaume
2018-2-27
Can you explain a lot better what it is you want to do. For your given file, what should the output be?
回答(1 个)
Rik
2018-2-27
If you have the Image Processing Toolbox, you can use bwlabel to label each group of indices where z is 1. If you don't have this toolbox, this wikipedia page could help.
data=xlsread('absbreak.xlsx');
v=data(:,2);
z=data(:,3);
[labels,num]=bwlabel(z==1);
avg_vec=zeros(1,num);
for n=1:num
avg_vec(n)=mean(v(labels==n));
end
1 个评论
Rik
2018-3-9
If you found this answer useful, please mark it as accepted answer. It will give me reputation points and will make it easier for other people with the same question to find an answer.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!