printing matrix with real and complex numbers

I have a matrix containg some real numbers and some imaginary numbers (eigenvalues).
I used following code to print to file
fprintf(fid,'%f %f\n',real(eigenValues), imag(eigenValues));
This code is ok if all are imaginary numbers. When some elements are only real, this code takes adjacent element and says that it is a imaginary. For eg.
Eigenvalue=
-203.122425981375 + 0.00000000000000i
-0.378775249508577 + 81.5733975555839i
-0.378775249508577 - 81.5733975555839i
-1.52421833698961 + 58.2049500388907i
-1.52421833698961 - 58.2049500388907i
-2.35088199995723 + 20.1918127788066i
-2.35088199995723 - 20.1918127788066i
-10.5006520053745 + 0.00000000000000i
But output to the file is
-203.122426 -0.378775
-0.378775 -1.524218
-1.524218 -2.350882
-2.350882 -10.500652
0.000000 81.573398
-81.573398 58.204950
-58.204950 20.191813
-20.191813 0.000000
Please help. fast.

Réponses (2)

Your code is not correct even if everything has non-zero imaginary coefficients. You need
fprintf(fid,'%f %f\n',[real(eigenValues(:)), imag(eigenValues(:)].' );
This code can be simplified if it is known for sure that eigenValues is a row vector. In that special case,
fprintf(fid,'%f %f\n',[real(eigenValuess); imag(eigenValues)] );
Remember, fprintf goes down columns when determining what to print next.

1 commentaire

You didn't really say what you wanted to do with the larger matrix that combines real and imaginary values, so I had to guess. This code notices when entire columns are real-valued and omits printing the imaginary parts for those columns.
Letting your matrix be MAT,
formats = {'%f\t', '%f %+fi\t'}; %you can use ' ' instead of '\t'
rMAT = real(MAT);
iMAT = imag(MAT);
needs_imag = any(iMAT); %columnwise test if any imaginary component
T = formats(needs_imag + 1); %those ones get different format
fmt = [T{:}];
fmt(end) = sprintf('\n');
jointMAT = reshape([rMAT; iMAT], size(MAT,1), []);
jointselector = reshape( [true(1,size(MAT,2)); needs_imag], 1, []);
joint_to_emit = jointMAT(:, jointselector);
fprintf( fmt, joint_to_emit.' );

Connectez-vous pour commenter.

nirmal joshi
nirmal joshi le 9 Déc 2013
Modifié(e) : Walter Roberson le 9 Déc 2013
thanks that helped a lot. Now could you please tell me the solution for the matrix of such kind as below (n*n matrix with mixed real and real+imag numbers):
0.00492313930954967 + 0.00000000000000i -0.000950219597881420 + 0.000116334561424967i -0.000950219597881420 - 0.000116334561424967i 0.00250610909631282 + 0.000131274110324684i 0.00250610909631282 - 0.000131274110324684i -0.00878994543063407 - 0.00290189331280875i -0.00878994543063407 + 0.00290189331280875i 0.0952321817243563 + 0.00000000000000i
0.000215354342101627 + 0.00000000000000i 0.000279237290606386 - 0.00885850893055061i 0.000279237290606386 + 0.00885850893055061i -0.000564605177494268 + 0.0162064497420263i -0.000564605177494268 - 0.0162064497420263i -0.00700806431294748 - 0.0239971293757875i -0.00700806431294748 + 0.0239971293757875i 0.0832384070238751 + 0.00000000000000i
9.42658185263975e-06 + 0.00000000000000i 0.000289914499551605 + 0.0120234671842429i 0.000289914499551605 - 0.0120234671842429i -0.00120988395040322 + 0.00453319372311672i -0.00120988395040322 - 0.00453319372311672i -0.00495242960762889 - 0.0398653109929623i -0.00495242960762889 + 0.0398653109929623i 0.0758949072105734 + 0.00000000000000i
5.56029916323066e-07 + 0.00000000000000i -0.000300371318816607 - 0.00763953150395058i -0.000300371318816607 + 0.00763953150395058i 0.000627704975245643 - 0.0148071554985026i 0.000627704975245643 + 0.0148071554985026i -0.00380520630845819 - 0.0473718174702857i -0.00380520630845819 + 0.0473718174702857i 0.0727914234389611 + 0.00000000000000i
-1.00000000000000 + 0.00000000000000i -0.00912988576329760 - 0.0775567056756187i -0.00912988576329760 + 0.0775567056756187i -0.0114606604719524 + 0.145667864336768i -0.0114606604719524 - 0.145667864336768i 0.0792586109697300 - 0.170662923716410i 0.0792586109697300 + 0.170662923716410i -1.00000000000000 + 0.00000000000000i
-0.0437432964133051 + 0.00000000000000i 0.722512902567027 + 0.0261337184494130i 0.722512902567027 - 0.0261337184494130i -0.942435015977903 - 0.0575649840220967i -0.942435015977903 + 0.0575649840220967i 0.501020675832976 - 0.0850911030487098i 0.501020675832976 + 0.0850911030487098i -0.874057545639903 + 0.00000000000000i
-0.00191475017462017 + 0.00000000000000i -0.980904881053617 + 0.0190951189463831i -0.980904881053617 - 0.0190951189463831i -0.262010186867606 - 0.0773308118839508i -0.262010186867606 + 0.0773308118839508i 0.816595473558801 - 0.00627988940137956i 0.816595473558801 + 0.00627988940137956i -0.796946009597802 + 0.00000000000000i
-0.000112942145521767 + 0.00000000000000i 0.623296313731386 - 0.0216086435525844i 0.623296313731386 + 0.0216086435525844i 0.860892986574868 + 0.0591048746527951i 0.860892986574868 - 0.0591048746527951i 0.965468460368975 + 0.0345315396310249i 0.965468460368975 - 0.0345315396310249i -0.764357406508843 + 0.00000000000000i

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by