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 (최근 30일)
이전 댓글 표시
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
dpb
2022년 6월 8일
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.
답변 (1개)
the cyclist
2022년 6월 8일
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])
and you can do similarly for the other patterns:
index10 = strfind(c,[1 0])
index11 = strfind(c,[1 1])
You should then be able to use those indices to index into your other array that you want to sum.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!