rewrite my loop - whats wrong with that loop ?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2016-9-7
How does this question differ from your previous question that looks a lot the same?
采纳的回答
Thorsten
2016-9-7
编辑:Thorsten
2016-9-8
I think you can rewrite your code as follows:
if dir1 == 1
addr = ADDR1; val = 1;
else
addr = N_SM - ADDR1; val = 0;
end
if I1 > 0
[~, idx] = sort(V1, 'ascend');
else
[~, idx] = sort(V1, 'descend');
end
idx = idx(find(SMh1(idx) == 0, 1, 'first'));
% use addr, val, and idx:
if ADDR1 >= 1 && ADDR1 < 20
SMh1(idx) = val;
elseif ADDR1 == 20
SMh1(:) = val;
end
% output
SMh1
Then it is much easier to debug under which condition the code does not do what you intend to do.
6 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!