I am trying to implement a function mapping shown in attached image but not working, pls correct me
조회 수: 1 (최근 30일)
이전 댓글 표시
%{
my code not working ,pls correct me
question:
for i=0:1:15 output(A)
'0000' 00
'0001' 01
'0010' 10
'0011' 11
'0100' 11
'0101' 10
'0110' 01
'0111' 00
'1000' 00
'1001' 01
'1010' 10
'1011' 11
'1100' 11
'1101' 10
'1110' 01
'1111' 00
if msb-1==0; output=i(msb-2:end);
else msb-1==1; output=~i(msb-2:end); i.e., ones comp of i(msb-2:end)
%}
N=16;
m= 2^(log2(N)-2); % m=4
A=zeros(1,m);
j=zeros(1,m);
k=zeros(1,m);
for i=0:1:N-1
msb1= bitget(i,log2(N)-1); %msb1=msb-1
if(msb1==0)
j(i+1)=string(dec2bin(i)).extractAfter(1);
A(i+1)=bin2dec(j);
else
j(i+1)=string(dec2bin(i)).extractAfter(1);
k(i+1)=not(j-'0');
A(i+1)=bin2dec(k);
end
end
댓글 수: 3
답변 (1개)
ag
2023년 11월 10일
편집: ag
2023년 11월 10일
Hi VARDIREDDY,
I understand that you need to implement the following logic:
if the 2nd MSB is 0, output= 3rd MSB to end, else output = One’s complement of 3rd MSB to end.
I’ve made a few changes to your code, as shown below:
N=16;
m= 2^(log2(N)-2); %m = 4
A = ['']; %empty character array for result
for i = 0:N-1
binStr = dec2bin(i, m); % Convert number to binary string
remStr = extractAfter(binStr, 2); %extract the 3rd MSB to end string
if(binStr(2)=='0') % check if the 2nd MSB is 0
A = [A; remStr]; % append the result to answer array
else
A = [A; char(not(remStr-'0') + '0')]; %append the one’s complement of the result to the answer array
end
end
A
For more details, please refer to the following documentations:
- https://www.mathworks.com/help/symbolic/sym.dec2bin.html
- https://www.mathworks.com/help/matlab/ref/extractafter.html#:~:text=extractAfter(str%2Cpat)-,newStr%20%3D%20extractAfter(str%2Cpos),-Description
Hope this helps!
Best Regards,
Aryan Gupta
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!