whats wrong in that program?

2 次查看(过去 30 天)
itsik
itsik 2011-5-10
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

回答(2 个)

Walter Roberson
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
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
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 CenterFile 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!

Translated by