whats wrong in that program?
조회 수: 2 (최근 30일)
이전 댓글 표시
im trying to write a matlab cod that find in random two varabels if its 00 --> 1 01 --> 1+i 10 --> -1 11 --> -1-i and the matrix ipMod gets the value in matched like if : ip= [0,1] then c= 1+i ? tried to make it with " if not equal" but i cant find my mistake thank u so much!! and this is the cod:
clear all
j=sqrt(-1)
nBit=2500
ip1 = rand(1,nBit) >0.5;
ip2=rand(1,nBit) >0.5;
ip=[ip1;ip2]
% QBPSK modulation
% bit00 --> 1
% bit01 --> 1+j
% bit10 -->-1
% bit11 -->-1-j
for q1=1:nBit
q2=1:nBit
if ip1(q1)~=1 & ip2(q2)~=1
ipMod=1
else
if ip1(q1)~=1 & ip2(q2)~=0
ipMod=1+j
else
if ip1(q1)~=0 & ip2(q2)~=1
ipMod=-1
else
if ip1(q1)~=0 & ip2(q2)~=0
ipMod=-1-j
end
end
end
end
end
댓글 수: 0
답변 (2개)
Walter Roberson
2011년 5월 10일
You are not using a "for" loop over q2, so q2 is being set to a vector of values. Your "if" statements are then trying to process a vector of true and false conditions. The rule for "if" statements is that a vector (or matrix) of logical values is considered true if and only if all() of the vector (or matrix) is true. For example your statement
if ip1(q1)~=1 & ip2(q2)~=1
is interpreted as
if all(ip1(q1)~=1 & ip2(q2)~=1)
Glancing at your code, I think you want q2=q1
댓글 수: 2
Walter Roberson
2011년 5월 10일
Also, you keep overwriting ipMod, so with your code only the final loop will have effect. You would want to store in to ipMod(q1).
By the way, the entire encoding loop can be replaced with:
ipMod = 1 - 2 * ip1 - 1i * ip2;
That's all that is needed, no "if" and no looping, just one statement.
Andrei Bobrov
2011년 5월 11일
more variant
nBit = 2500;
ipMod = zeros(1,nBit);
ip1 = rand(1,nBit) >0.5;
ip2 = rand(1,nBit) >0.5;
t1 = ip1==1;
t2 = ip2==1;
ipMod(t1 & t2) = -(1+1i);
ipMod(~t1 & t2) = 1+1i;
ipMod(t1 & ~t2) = -1;
ipMod(~t1 & ~t2) = 1;
참고 항목
카테고리
Help Center 및 File Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!