フィルターのクリア

Using for loop to access two elements from array

7 ビュー (過去 30 日間)
Samantha Plesce
Samantha Plesce 2021 年 3 月 22 日
編集済み: Matt J 2021 年 3 月 22 日
I am trying to convert an array of bit values into complex values. For every two elements the first is designated as the real part and the second as the imaginary part. I am confused how to access the current index and the next index without exceeding the number of array elements.
str='abc';
str_dec=double(str); %converts to decimal
str_bin=dec2bin(str_dec,8) %converts string from decimal to binary
bits=reshape(str_bin.',1,8*length(str))
QPSK = [];
%grab every two elements from bits to convert to QPSK
for i = length(bits)
%convert bits to complex values
if bits(i) > 1
if bits(i+1) > 1
QPSK = [QPSK, 1+1j];
else
QPSK = [QPSK, 1-1j];
end
else
if bits(i+1) > 1
QPSK = [QPSK, -1+1j];
else
QPSK = [QPSK, -1-1j];
end
end
end
QPSK

回答 (1 件)

Matt J
Matt J 2021 年 3 月 22 日
編集済み: Matt J 2021 年 3 月 22 日
Forget the loop. It's a four-line operation:
str='abc';
bits=reshape( dec2bin(str,8).' ,1,[]) -'0'; %converts string from decimal to binary
parts=2*bits-1;
QPSK=complex( parts(1:2:end) , parts(2:2:end) )
QPSK =
-1.0000 + 1.0000i 1.0000 - 1.0000i -1.0000 - 1.0000i -1.0000 + 1.0000i -1.0000 + 1.0000i 1.0000 - 1.0000i -1.0000 - 1.0000i 1.0000 - 1.0000i -1.0000 + 1.0000i 1.0000 - 1.0000i -1.0000 - 1.0000i 1.0000 + 1.0000i

カテゴリ

Help Center および File ExchangeQPSK についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by