rewrite my loop - whats wrong with that loop ?

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 Comment

How does this question differ from your previous question that looks a lot the same?

Sign in to comment.

 Accepted Answer

Thorsten
Thorsten on 7 Sep 2016
Edited: Thorsten on 8 Sep 2016
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 Comments

Hi, thanks you for your answer. I thinkit is not works as I mentioned before/ for example:
if
V1 = [32 19 38 27 73 72 87 81 39 65 44 84 72 18 29 86 55 43 77 87];
then
SMhl = 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
and if V1 change to :
V1 = [21 31 11 12 13 25 26 61 53 35 24 14 57 82 49 66 59 33 64 91];
then
SMhl =0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
It looks that it passedon value 11 --- location number 3..
it should be:
SMhl = 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
can we did it ?
Henry
I change the code above and ran the example; now the results is as desired:
ADDR1 = 1; dir1 = 1; I1 = 1; % I1 >0
V1 = [32 19 38 27 73 72 87 81 39 65 44 84 72 18 29 86 55 43 77 87];
SMh1 = zeros(size(V1));
% run code
V1 = [21 31 11 12 13 25 26 61 53 35 24 14 57 82 49 66 59 33 64 91];
% run code
Output
SMh1 =
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
Hi,
What is the best way to change it to what I need as I mentiond above ?
Can you helps me to fix it ?
Thanks, Henry
Thorsten
Thorsten on 7 Sep 2016
Edited: Thorsten on 7 Sep 2016
What is the problem with the code I posted below. I must confess that I have not completely understood what you want to do in the different cases. It would be most helpful if you run my code and point towards different combinations of parameters that do not works as expected, if you cannot correct my code for these cases yourself.
Hi, Thanks again,
Almost as I expected...
ADDR1 = 1; dir1 = 1; I1 = 1; % I1 >0
V1 = [32 19 38 27 73 72 87 81 39 65 44 84 72 18 29 86 55 43 77 87];
SMh1 =
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
ADDR1 = 2; dir1 = 1; I1 = 1; % I1 >0
V1 = [21 31 11 12 13 25 26 61 53 35 24 14 57 82 49 66 59 33 64 91];
SMh1 =
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
If I return to v1:
ADDR1 = 3; dir1 = 1; I1 = 1; % I1 >0
V1 = [32 19 38 27 73 72 87 81 39 65 44 84 72 18 29 86 55 43 77 87];
SMh1 =
SMh1 =
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
That is the problem. It should set the bit who belong to value 19...
That mean SMh1 need to be:
SMh1 =
0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
And so on for the next ADDR1 and V1.
Again, if the bit of SMh1 is set to 1 (after the function found the min value of that bit), in the next ADDR1 and V1 it will set the bit with the next min. If the next min is on the same location of SMh1 bit(who's alrwady set to 1), it will search the next min of V1 and will set the index of it on SMh1.
I hope that my explanation is clearly.
Thanks, Henry
Thorsten
Thorsten on 8 Sep 2016
Edited: Thorsten on 8 Sep 2016
I see. I've updated the code above such that it produces the desired result. Are there any other conditions that do not work? Otherwise, please accept my answer.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 7 Sep 2016

Edited:

on 8 Sep 2016

Community Treasure Hunt

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

Start Hunting!