Do calculations for lines that are true

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 commentaires

jonas
jonas le 27 Fév 2018
And what do you want to do when the z(1)==1? There is no previous line.
Dylan Mecca
Dylan Mecca le 27 Fév 2018
It can be disregarded. I understand that there can't be a calculation there.
Dylan Mecca
Dylan Mecca le 27 Fév 2018
I'll change the document to have that z(1) to be a zero to make answering this question a little easier. It isn't critical to my application.
jonas
jonas le 27 Fév 2018
Modifié(e) : jonas le 27 Fév 2018
abs=xlsread('absbreak.xlsx');
v=abs(:,2)
z=abs(:,3)
ind=find(z==1)
v(ind)=v(ind)-v(ind-1)
I probably misinterpreted your request. Do you want v to update while you loop over the data?
Dylan Mecca
Dylan Mecca le 27 Fév 2018
Modifié(e) : Dylan Mecca le 27 Fév 2018
'v' should not be updated, but the answers from the calculation derived from 'v' should be averaged then stored into another vector.
On a side note, can I ask why you suggest using abs?
jonas
jonas le 27 Fév 2018
'abs' is just a poorly chosen variable name that I got from the name of the excel-file, it has nothing to do with the function abs().
If you want to average the data then I suggest that you use find(z==0), which will give you 4 indices over which you can then average.
There is probably a much better way but I am still not entirely sure what you want to do. For example, do you want to include the v-values of z being zero? Otherwise the average is obviously 1.
Guillaume
Guillaume le 27 Fév 2018
Can you explain a lot better what it is you want to do. For your given file, what should the output be?
Dylan Mecca
Dylan Mecca le 27 Fév 2018
Output should have a new vector of average 'v' only where z = 1.
For instance, cells 3:9 have 'z' as true. Take the average of 'v' during that interval, and store that average value as the first value in a new vector.
Do the same for cells 11:16, where that average will be stored as the second value in the new vector.
And finally, do it for a third vector value for average value of 'v' where 'z' = 1 in cells 19:22.
I can build a hard code to satisfy these cell values, but I need help developing a way to make matlab see the pattern I', trying to explain.

Connectez-vous pour commenter.

Réponses (1)

Rik
Rik le 27 Fév 2018
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 commentaire

Rik
Rik le 9 Mar 2018
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.

Connectez-vous pour commenter.

Catégories

Commenté :

Rik
le 9 Mar 2018

Community Treasure Hunt

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

Start Hunting!

Translated by