check each element of random vector c=[0, 1, 0, 1,.....n] one by one if value of current element is 0 and previous element is 0 perform this action otherwise another action.

1 view (last 30 days)
I want to run for loop to Check each element of random vector c=[0, 1, 0, 1,.....n] if value of 1st element is 0 and next element is 0 perform summation of certian values, if 1st element is 0 and next element is 1 then perform summation of certain values,if 1st element is 1 and next element is 0 then perform summation of certain values and if 1st element is 1 and next element is 1 then perform summation of certain values. This will continue till end so we need to check the element and the previous element.
function [Total_delay_upper, Total_delay_lower] = f_Cummulative_delay(c,T_pUU,T_pUL,T_pLL,T_pLU)
Total_delay_upper=0;
Total_delay_lower=0;
for i=1:length(c)
if c(i)==0
Total_delay_upper = T_pUU;
Total_delay_lower = T_pLL;
elseif c(i)==1
Total_delay_upper = T_pUL;
Total_delay_lower = T_pLU;
end
if c(i)==0&&c(i+1)==0
Total_delay_upper = Total_delay_upper+T_pUU;
Total_delay_lower = Total_delay_lower+T_pLL;
elseif c(i)==0&&c(i+1)==1
Total_delay_upper = Total_delay_upper+T_pUL;
Total_delay_lower = Total_delay_lower+T_pLU;
end
if c(i)==1&&c(i+1)==0
Upperdelay = Upperdelay(i)+T_pUL;
Lowerdelay = Lowerdelay(i)+T_pLU;
  8 Comments
dpb
dpb on 8 Jun 2022
Well, it makes no sense at all then...you're going to have to illustrate precisely what you think the result should be for a given case; I'm guessing you can't write the code because you don't have a complete-enough definition written precisely enough to define the algorithm.

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 8 Jun 2022
I didn't fully understand what you are trying to do with your code, but I think the following will help you.
To find all the locations of the pair [0 1] in a vector c, you can simply do
c = [0 1 0 1 1 0 1 0 1 0 1 1];
index01 = strfind(c,[0 1])
index01 = 1×5
1 3 6 8 10
and you can do similarly for the other patterns:
index10 = strfind(c,[1 0])
index10 = 1×4
2 5 7 9
index11 = strfind(c,[1 1])
index11 = 1×2
4 11
You should then be able to use those indices to index into your other array that you want to sum.

Categories

Find more on Creating and Concatenating Matrices 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!