how to make a PN code?
4 views (last 30 days)
Show older comments
I make a PN code for home work. but it's not work but i don't know the reason why. what's the problem n how to build PN code in base on my code. It's my code i know the answer, num_right has to be 60.
var= dec2bin(0:2^9-1)-'0';
var = [ones(512,1),var,ones(512,1)];
num_right = 0;
for k=1 : 512
if(mod(sum(var(k,:)),2)==1)
org(1,:)= var(k,:);
for l=1 :1022
check(k,l) = org(l,3);
next_high = rem(sum(org(l,2:11)),2);
org(l+1,:) = circshift(org(l,:),1);
org(l+1,1) = next_high;
end
check(k,1023) = org(1023,3);
if(isequal(org(1023,:),var(k,:))==1)
for m=1:1023
if(check(k,m)==1)
check(k,m) = -1;
end
if(check(k,m) ==0)
check(k,m) = 1;
end
end
for n=1:1023
check_change = circshift(check(k,:),n);
check_sum(k,n) = sum(check(k,:).*check_change);
end
if (check_sum(k,1023) ==1023)
sum_check = check_sum(k,1:1022).*check_sum(k,1:1022);
if(sum(check_sum(k,1:1022).*check_sum(k,1:1022))==1022)
fprintf('%g \n',var(k,:));
num_right = num_right + 1;
end
if(sum(check_sum(k,1:1022).*check_sum(k,1:1022))~=1022)
continue, end
if (check_sum(k,1023) ~=1023)
continue, end
end
end
end
end
fprintf('%g \n', num_right);
0 Comments
Answers (1)
Dimitris Iliou
on 16 Jun 2017
The problem with your code is that the
if(sum(check_sum(k,1:1022).*check_sum(k,1:1022))==1022)
is never true. Due to that, your num_right variable is never modified.
If you debug the code, you will find that the first time you hit that if statement you have the following:
sum(check_sum(k,1:1022).*check_sum(k,1:1022)
ans =
103968206
and
size(check_sum(k,1:1022).*check_sum(k,1:1022))
ans =
1 1022
Having said that, I think you might be checking for the wrong thing.
What I would suggest is try debugging your code in order to understand how it works and what you can do to change it.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!