Converting PSD to Time Domain Signal I am currently using MATLAB and I'm thinking if I can reconstruct PSD back to time domain signal using MATLAB's ifft. I found a link in solving this problem but it's in Phyton and when I tried it in MATLAB, I encountered an error mentioning that the dimensions didn't match. This is the code that I come up with: 

%PSD to time-domain signal

%periodogram using fft
Fs = 1000;
t = 0:1/Fs:1-1/Fs;
x = cos(2*pi*100*t) + randn(size(t));

N = length(x);
xdft = fft(x);
xdft = xdft(1:N/2+1);
psdx = (1/(Fs*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
freq = 0:Fs/length(x):Fs/2;

figure(1)
plot(freq,10*log10(psdx))
grid on
title('Periodogram Using FFT')
xlabel('Frequency (Hz)')
ylabel('Power/Frequency (dB/Hz)')

figure(2)
plot(t,x)
grid on
title ('Signal')

%psd to signal using ifft
magnitude = N*sqrt(psdx);
phase = 2*pi*randn(1,N);
FFT = magnitude .* exp(sqrt(-1) .* phase);
signal = ifft(FFT);

figure(3)
plot(t,signal)


I calculated the PSD using the FFT but its just so that I can do it backward from PSD to FFT and then iFFT it to find the signal. This is just an example for me to compare if doing it backwards can result in the exact same signal as the original one. But once again, MATLAB said there's an error, is there anything I can change in my code? Thank you.
@Amelia. Hi. I tried to run your MATLAB code, but it contains several errors regarding vector lengths and produces error messages. But that's not the main issue here. The answer to your question is: If you compute the DFT of an N-point x(n) sequence you produce an N-point complex-valued xDFT(m) frequency-domain sequence. You can only reconstruct the original x(n) sequence by computing the inverse DFT of the N-point xDFT(m) sequence. You cannot recover the original N-point x(n) sequence by computing the inverse DFT of any sequence that is not the xDFT(m) sequence.