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 日間)
Vaseem Khan
Vaseem Khan 2022 年 6 月 8 日
コメント済み: dpb 2022 年 6 月 8 日
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 件のコメント
Vaseem Khan
Vaseem Khan 2022 年 6 月 8 日
I really mean the initials, my final value and total path depends on the intial value.
dpb
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
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])
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.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by