How can the generated complex numbers represented by j instead of i?

조회 수: 13 (최근 30일)
Lz Lu
Lz Lu 2020년 6월 10일
댓글: Jason Ellison 2023년 4월 25일
I need to import data into python. However, imaginary number does not use i in python
  댓글 수: 2
Stephen23
Stephen23 2020년 6월 10일
You can save the data in a .mat file and then use numpy/scipy loadmat.
Lz Lu
Lz Lu 2020년 6월 18일
Sorry,I first need to import the data into a csv file before I can proceed to the next step

댓글을 달려면 로그인하십시오.

답변 (2개)

the cyclist
the cyclist 2020년 6월 18일
If z is your complex variable in MATLAB, save the real part
real(z)
and the imaginary part
imag(z)
separately, and re-construct them in python.

Jason Ellison
Jason Ellison 2023년 4월 25일
Hi!
I had this problem recently too. I fixed it in Python instead of MATLAB. Here is my solution.
# this one imports a csv and compares it to the output of the function above. This is quite painful in python.
# First, you need to open the file and read the lines into a variable.
with open('./csv_files/S2_M12_transmissionFrom1x_shiftn3_lfl2_rfl2_alpha0p5_matlab.csv') as f:
lines = f.readlines()
# initialize array
known = zeros((len(lines),), dtype='complex')
# then, in a loop, you need to
k = 0 # this will index the known array
for line in lines:
# 1. find where the 'i' character is.
m = re.search('i', line)
n = m.span()
# 2. replace 'i' with 'j'
number_in_string = line[0:n[0]]+'j'
# 3. convert to complex and put it in an array
known[k] = complex(number_in_string)
k = k+1
  댓글 수: 1
Jason Ellison
Jason Ellison 2023년 4월 25일
I have an alternative answer in MATLAB as well ...
fid = fopen("filename.csv", 'w');
n = length(data);
for i = 1:n
if i~=n
% write all but the last line like this
fprintf(fid, '%d+%dj,',real(data(i)), imag(data(i)));
else
% on the last line, don't put a comma
fprintf(fid, '%d+%dj',real(data(i)), imag(data(i)));
end
end
fclose(fid);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by