필터 지우기
필터 지우기

rewrite my loop - whats wrong with that loop ?

조회 수: 1 (최근 30일)
Henry Buck
Henry Buck 2016년 9월 7일
편집: Thorsten 2016년 9월 8일
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
Walter Roberson 2016년 9월 7일
How does this question differ from your previous question that looks a lot the same?

댓글을 달려면 로그인하십시오.

채택된 답변

Thorsten
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
Henry Buck
Henry Buck 2016년 9월 7일
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 2016년 9월 8일
편집: Thorsten 2016년 9월 8일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by