rewrite my loop - whats wrong with that loop ?
Afficher commentaires plus anciens
Hi, I did rewrite my code - matlab functionas you can see below:
_if(dir1==1)
if(I1>0)
if(ADDR1==1)
[~, I] = sort(V1,'ascend');
SMh1(I(1:ADDR1)) = 1;
elseif(ADDR1>1 && ADDR1<20)
[~, I] = sort(V1,'ascend');
SMh1(I(ADDR1:ADDR1)) = 1;
elseif(ADDR1==20)
SMh1(:) = 1;
end
else
if(ADDR1==1)
[~, I] = sort(V1,'descend');
SMh1(I(1:ADDR1)) = 1;
elseif(ADDR1>1 && ADDR1<20)
[~, I] = sort(V1,'descend');
SMh1(I(ADDR1:ADDR1)) = 1;
elseif(ADDR1==20)
SMh1(:) = 1;
end
end
else
if(I1>0)
if(ADDR1==19)
[~, I] = sort(V1,'ascend');
SMh1(I(1:N_SM-ADDR1)) = 0;
elseif(ADDR1<19 && ADDR1>1)
[~, I] = sort(V1,'ascend');
SMh1(I(N_SM-ADDR1:N_SM-ADDR1)) = 0;
elseif(ADDR1==0)
SMh1(:) = 0;
end
else
if(ADDR1==19)
[~, I] = sort(V1,'descend');
SMh1(I(1:N_SM-ADDR1)) = 0;
elseif(ADDR1<19 && ADDR1>1)
[~, I] = sort(V1,'descend');
SMh1(I(N_SM-ADDR1:N_SM-ADDR1)) = 0;
elseif(ADDR1==0)
SMh1(:) = 0;
end
end
end_
The input to that function are: ADDR1,dir1,I1,V1
The output is SMh1.
V1 is kind a vector of 20 elements, like V1=[23 42 7 12 93...61 90].
V1 is input and it changing as far as ADDR1 increasing/decreasing.
SMhl is the output of that function.
SMh1 is a vector of 20 elements and should filled with 0 or 1 depending on the code above.
for example:
if ADDR1 = 0
SMhl = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] -> not depend on values of V1.
when ADDR1 = 1... there is dependent on V1,dir1 and I1:
for dir1=1 and I1>0 the function should search the first min values fo V1, and indicate the SMh1 to 1.
The index of that bit should be the index of min V1:
for example:
*ADDR1 = 1*
dir1=1 and I1>0
V1 = [84 82 21 25 26 54 57 74 43 61 98 87 33 66 49 71 *19* 80 30 51]
SMhl [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 *1* 0 0 0]
*ADDR1 = 2*
dir1=1 and I1>0
V1 = [14 22 33 55 76 54 37 44 43 51 28 37 33 86 99 61 29 43 *10* 31]
SMhl = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 *1* 0 *1* 0]
*ADDR1 = 3*
dir1=1 and I1>0
V1 = [ *34* 44 55 66 77 88 84 62 87 47 90 72 68 51 39 79 93 82 101 131]
SMhl = [ *1* 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 *1* 0 *1* 0]
..................................
..................................
..................................
*ADDR1 = 20*
dir1=1 and I1>0
V1 = [34 44 55 66 77 88 84 62 87 47 90 72 68 51 39 79 93 82 101 131]
SMhl = [ *1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1*]
the algorytm should be like that: ---> depending on the location of min V1(the index). when dir1=1, search min values and set SMhl bit to 1 when dir1=0, search min values and set SMhl bit to 0 when I1<0 --> the function do the same but search for mav V1.
Somthing does not works proper in that function. I am stuck and really need for help.
Can soeone help me to solve it ?
Thanks, Henry
1 commentaire
Walter Roberson
le 7 Sep 2016
How does this question differ from your previous question that looks a lot the same?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Simulink Environment Customization dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!