MATLAB: Changing real part of complex number complexreal I have an array of compelx numbers. I'd like to change the real part of the 34th array member to zero. This code doesn't work: real( my_complex_array(34) ) = 0; What's the solution? Best Answer X = (1:35)' * (1+1i); % not being very creative hereNow, there are lots of ways to just kill the real part of only the 34'th element. This will work:X(34) = X(34) - real(X(34));Perhaps simpler, because it will not require the subtraction.X(34) = 1i*imag(X(34));Or,X(34) = 0 + 1i*imag(X(34));Another one, using complex.X(34) = complex(0,imag(X(34));In any event, did it work?X(33:35)ans = 33 + 33i 0 + 34i 35 + 35iOf course it did. Related SolutionsMATLAB: How to threshold only real part of a complex matrix >> A = complex(randi([-3,3],4,4),randi([-3,3],4,4)) % fake dataA = -2 + 3i -2 + 1i 2 - 3i -2 + 1i -2 + 0i 3 + 0i -3 + 3i 0 + 0i 2 + 0i 2 - 2i 3 + 3i 0 - 3i 1 - 2i -3 - 2i 3 + 2i 0 - 2i>> B = complex(real(A).*(real(A)<=0.5),imag(A)) % set real(A)>0.5 to 0.B = -2 + 3i -2 + 1i 0 - 3i -2 + 1i -2 + 0i 0 + 0i -3 + 3i 0 + 0i 0 + 0i 0 - 2i 0 + 3i 0 - 3i 0 - 2i -3 - 2i 0 + 2i 0 - 2i MATLAB: Mat2str() on complex values with 0 inaginary part Mathematically 5 is the same as 5+0i, so the output is correct.So omitting the imaginary part is a question of taste.If you want to display it, do this expliticitly:x = 1 + 2i;sprintf('%g %gi', real(x), imag(x))Alternatively you can copy the code of mat2str to e.g. mat2stri anywhere in your user-defined folders in the path and remove the line:imagStr(imagVal == 0) = ""; Related QuestionHow to take an integral of a matrixI cannot create that array in Matlab !
Best Answer