Everything being the same, then why does matrix C give different values in the two codes?

1 view (last 30 days)
All the values are the same in both the codes, then why does matrix C give different values in both the codes?
code1:
u=[30 50 110];
M=10;
N=4;
K=3;
d=0.5;
fn=@(u,k) exp(1j*2*pi*d*(0:k-1).' * sind(u));
A=fn(u,M);
B=fn(u,N);
C=kron(B,A);
code2
u=[30 50 110];
M=10;
N=4;
K=3;
d=0.5;
C = STM(u,M,N,d);
function C = STM(u,M,N,d)
A=exp(1j*2*pi*d*(0:M-1).'*sind(u));
B=exp(1j*2*pi*d*(0:N-1).'*sind(u));
C = zeros(size(A, 1)*size(B, 1), length(u));
for idxK = 1 : 1 : length(u)
C(:, idxK) = kron(B(:, idxK), A(:, idxK));
end
end

Answers (1)

Cris LaPierre
Cris LaPierre on 16 Oct 2021
Because they are not the same?
Your output should be [size(A, 1)*size(B, 1), size(A, 2)*size(B, 2)]
See the More About > Kroeneker Tensor Product for what kron is calculating.
  3 Comments
Cris LaPierre
Cris LaPierre on 16 Oct 2021
Then you misunderstand what the Kroeneker Tensor Product is.
C is a 40x9 matrix in your first code.
u=[30 50 110];
M=10;
N=4;
K=3;
d=0.5;
fn=@(u,k) exp(1j*2*pi*d*(0:k-1).' * sind(u));
A=fn(u,M);
B=fn(u,N);
C=kron(B,A)
C =
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 0.0000 + 1.0000i -0.7418 + 0.6706i -0.9821 + 0.1883i 0.0000 + 1.0000i -0.7418 + 0.6706i -0.9821 + 0.1883i 0.0000 + 1.0000i -0.7418 + 0.6706i -0.9821 + 0.1883i -1.0000 + 0.0000i 0.1006 - 0.9949i 0.9291 - 0.3699i -1.0000 + 0.0000i 0.1006 - 0.9949i 0.9291 - 0.3699i -1.0000 + 0.0000i 0.1006 - 0.9949i 0.9291 - 0.3699i -0.0000 - 1.0000i 0.5925 + 0.8056i -0.8428 + 0.5383i -0.0000 - 1.0000i 0.5925 + 0.8056i -0.8428 + 0.5383i -0.0000 - 1.0000i 0.5925 + 0.8056i -0.8428 + 0.5383i 1.0000 - 0.0000i -0.9797 - 0.2003i 0.7263 - 0.6874i 1.0000 - 0.0000i -0.9797 - 0.2003i 0.7263 - 0.6874i 1.0000 - 0.0000i -0.9797 - 0.2003i 0.7263 - 0.6874i 0.0000 + 1.0000i 0.8611 - 0.5084i -0.5839 + 0.8118i 0.0000 + 1.0000i 0.8611 - 0.5084i -0.5839 + 0.8118i 0.0000 + 1.0000i 0.8611 - 0.5084i -0.5839 + 0.8118i -1.0000 + 0.0000i -0.2978 + 0.9546i 0.4205 - 0.9073i -1.0000 + 0.0000i -0.2978 + 0.9546i 0.4205 - 0.9073i -1.0000 + 0.0000i -0.2978 + 0.9546i 0.4205 - 0.9073i -0.0000 - 1.0000i -0.4192 - 0.9079i -0.2421 + 0.9702i -0.0000 - 1.0000i -0.4192 - 0.9079i -0.2421 + 0.9702i -0.0000 - 1.0000i -0.4192 - 0.9079i -0.2421 + 0.9702i 1.0000 - 0.0000i 0.9198 + 0.3924i 0.0551 - 0.9985i 1.0000 - 0.0000i 0.9198 + 0.3924i 0.0551 - 0.9985i 1.0000 - 0.0000i 0.9198 + 0.3924i 0.0551 - 0.9985i 0.0000 + 1.0000i -0.9455 + 0.3257i 0.1340 + 0.9910i 0.0000 + 1.0000i -0.9455 + 0.3257i 0.1340 + 0.9910i 0.0000 + 1.0000i -0.9455 + 0.3257i 0.1340 + 0.9910i 0.0000 + 1.0000i 0.0000 + 1.0000i 0.0000 + 1.0000i -0.7418 + 0.6706i -0.7418 + 0.6706i -0.7418 + 0.6706i -0.9821 + 0.1883i -0.9821 + 0.1883i -0.9821 + 0.1883i -1.0000 + 0.0000i -0.6706 - 0.7418i -0.1883 - 0.9821i -0.6706 - 0.7418i 0.1006 - 0.9949i 0.6023 - 0.7983i -0.1883 - 0.9821i 0.6023 - 0.7983i 0.9291 - 0.3699i -0.0000 - 1.0000i 0.9949 + 0.1006i 0.3699 + 0.9291i 0.7418 - 0.6706i 0.5925 + 0.8056i -0.4412 + 0.8974i 0.9821 - 0.1883i 0.0885 + 0.9961i -0.8428 + 0.5383i 1.0000 - 0.0000i -0.8056 + 0.5925i -0.5383 - 0.8428i 0.6706 + 0.7418i -0.9797 - 0.2003i 0.2642 - 0.9645i 0.1883 + 0.9821i -0.7336 - 0.6796i 0.7263 - 0.6874i 0.0000 + 1.0000i 0.2003 - 0.9797i 0.6874 + 0.7263i -0.7418 + 0.6706i 0.8611 - 0.5084i -0.0779 + 0.9970i -0.9821 + 0.1883i 0.9999 + 0.0122i -0.5839 + 0.8118i -1.0000 + 0.0000i 0.5084 + 0.8611i -0.8118 - 0.5839i -0.6706 - 0.7418i -0.2978 + 0.9546i -0.1113 - 0.9938i -0.1883 - 0.9821i -0.7499 + 0.6615i 0.4205 - 0.9073i -0.0000 - 1.0000i -0.9546 - 0.2978i 0.9073 + 0.4205i 0.7418 - 0.6706i -0.4192 - 0.9079i 0.2964 + 0.9551i 0.9821 - 0.1883i 0.1127 - 0.9936i -0.2421 + 0.9702i 1.0000 - 0.0000i 0.9079 - 0.4192i -0.9702 - 0.2421i 0.6706 + 0.7418i 0.9198 + 0.3924i -0.4710 - 0.8821i 0.1883 + 0.9821i 0.5827 + 0.8127i 0.0551 - 0.9985i 0.0000 + 1.0000i -0.3924 + 0.9198i 0.9985 + 0.0551i -0.7418 + 0.6706i -0.9455 + 0.3257i 0.6287 + 0.7776i -0.9821 + 0.1883i -0.9772 - 0.2122i 0.1340 + 0.9910i -1.0000 + 0.0000i -0.3257 - 0.9455i -0.9910 + 0.1340i -0.6706 - 0.7418i 0.4830 - 0.8756i -0.7639 - 0.6453i -0.1883 - 0.9821i 0.8672 - 0.4979i -0.3182 - 0.9480i -1.0000 + 0.0000i -1.0000 + 0.0000i -1.0000 + 0.0000i 0.1006 - 0.9949i 0.1006 - 0.9949i 0.1006 - 0.9949i 0.9291 - 0.3699i 0.9291 - 0.3699i 0.9291 - 0.3699i -0.0000 - 1.0000i 0.7418 - 0.6706i 0.9821 - 0.1883i 0.9949 + 0.1006i 0.5925 + 0.8056i 0.0885 + 0.9961i 0.3699 + 0.9291i -0.4412 + 0.8974i -0.8428 + 0.5383i 1.0000 - 0.0000i -0.1006 + 0.9949i -0.9291 + 0.3699i -0.1006 + 0.9949i -0.9797 - 0.2003i -0.2745 - 0.9616i -0.9291 + 0.3699i -0.2745 - 0.9616i 0.7263 - 0.6874i 0.0000 + 1.0000i -0.5925 - 0.8056i 0.8428 - 0.5383i -0.9949 - 0.1006i 0.8611 - 0.5084i 0.4507 + 0.8927i -0.3699 - 0.9291i 0.8485 + 0.5292i -0.5839 + 0.8118i -1.0000 + 0.0000i 0.9797 + 0.2003i -0.7263 + 0.6874i 0.1006 - 0.9949i -0.2978 + 0.9546i -0.6108 - 0.7918i 0.9291 - 0.3699i -0.9843 + 0.1764i 0.4205 - 0.9073i -0.0000 - 1.0000i -0.8611 + 0.5084i 0.5839 - 0.8118i 0.9949 + 0.1006i -0.4192 - 0.9079i 0.7490 + 0.6626i 0.3699 + 0.9291i 0.6119 - 0.7909i -0.2421 + 0.9702i 1.0000 - 0.0000i 0.2978 - 0.9546i -0.4205 + 0.9073i -0.1006 + 0.9949i 0.9198 + 0.3924i -0.8604 - 0.5097i -0.9291 + 0.3699i 0.0764 + 0.9971i 0.0551 - 0.9985i 0.0000 + 1.0000i 0.4192 + 0.9079i 0.2421 - 0.9702i -0.9949 - 0.1006i -0.9455 + 0.3257i 0.9409 + 0.3386i -0.3699 - 0.9291i -0.7253 - 0.6884i 0.1340 + 0.9910i -1.0000 + 0.0000i -0.9198 - 0.3924i -0.0551 + 0.9985i 0.1006 - 0.9949i 0.4830 - 0.8756i -0.9879 - 0.1553i 0.9291 - 0.3699i 0.9997 + 0.0243i -0.3182 - 0.9480i -0.0000 - 1.0000i 0.9455 - 0.3257i -0.1340 - 0.9910i 0.9949 + 0.1006i 0.2289 + 0.9735i 0.9994 - 0.0335i 0.3699 + 0.9291i -0.7579 + 0.6523i 0.4910 + 0.8711i -0.0000 - 1.0000i -0.0000 - 1.0000i -0.0000 - 1.0000i 0.5925 + 0.8056i 0.5925 + 0.8056i 0.5925 + 0.8056i -0.8428 + 0.5383i -0.8428 + 0.5383i -0.8428 + 0.5383i 1.0000 - 0.0000i 0.6706 + 0.7418i 0.1883 + 0.9821i -0.8056 + 0.5925i -0.9797 - 0.2003i -0.7336 - 0.6796i -0.5383 - 0.8428i 0.2642 - 0.9645i 0.7263 - 0.6874i 0.0000 + 1.0000i -0.9949 - 0.1006i -0.3699 - 0.9291i -0.5925 - 0.8056i 0.8611 - 0.5084i 0.8485 + 0.5292i 0.8428 - 0.5383i 0.4507 + 0.8927i -0.5839 + 0.8118i -1.0000 + 0.0000i 0.8056 - 0.5925i 0.5383 + 0.8428i 0.8056 - 0.5925i -0.2978 + 0.9546i -0.9330 - 0.3600i 0.5383 + 0.8428i -0.9330 - 0.3600i 0.4205 - 0.9073i -0.0000 - 1.0000i -0.2003 + 0.9797i -0.6874 - 0.7263i 0.5925 + 0.8056i -0.4192 - 0.9079i 0.9841 + 0.1778i -0.8428 + 0.5383i 0.9335 - 0.3586i -0.2421 + 0.9702i 1.0000 - 0.0000i -0.5084 - 0.8611i 0.8118 + 0.5839i -0.8056 + 0.5925i 0.9198 + 0.3924i -0.9999 + 0.0107i -0.5383 - 0.8428i -0.4520 + 0.8920i 0.0551 - 0.9985i 0.0000 + 1.0000i 0.9546 + 0.2978i -0.9073 - 0.4205i -0.5925 - 0.8056i -0.9455 + 0.3257i 0.9800 - 0.1988i 0.8428 - 0.5383i -0.2628 - 0.9648i 0.1340 + 0.9910i -1.0000 + 0.0000i -0.9079 + 0.4192i 0.9702 + 0.2421i 0.8056 - 0.5925i 0.4830 - 0.8756i -0.9251 + 0.3798i 0.5383 + 0.8428i 0.8420 + 0.5395i -0.3182 - 0.9480i -0.0000 - 1.0000i 0.3924 - 0.9198i -0.9985 - 0.0551i 0.5925 + 0.8056i 0.2289 + 0.9735i 0.8370 - 0.5473i -0.8428 + 0.5383i -0.9864 + 0.1644i 0.4910 + 0.8711i 1.0000 - 0.0000i 0.3257 + 0.9455i 0.9910 - 0.1340i -0.8056 + 0.5925i -0.8226 - 0.5686i -0.7189 + 0.6951i -0.5383 - 0.8428i 0.6215 - 0.7834i -0.6463 - 0.7631i
size(C)
ans = 1×2
40 9
Sadiq Akbar
Sadiq Akbar on 19 Oct 2021
Thank you dear Cris LaPierre for your response. No you didn't take my point. I was saying that I want to make the response of the 1st code like that of 2nd. You tried but that is not according to my question. I wanted to make the response of 1st code like that of 2nd and I did that myself with several trial and errors. Thank you once again

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!