fprintf 13 values in a 5x4 matrix without zeros.

I first change my data set from a matrix to vector using the reshape
A = reshape(OriginalA, numel(A),1)
B = reshape(OriginalB, numel(A),1)
then found the intersection
C=sextor(A,B); (13 by 1 vector)
turnC into a 5x4 matrix ncol=4 nrow=ceil(numel(C)/ncol); for i=1:nrows for i=1:ncol if j == nrows extran=nrows-round(numel(C)/ncol) for k=1:extran D(nrows,k)=C((i-1)*ncol+k); else D(i,j)=C(i-1)*ncol+j); end end end
Now I want to print it to a file where there are 4 or 8 columns because that was what a separate program wants.
d1, d2, d3, d4,
d5, d6, d7, d8,
d9, d10, d11, d12,
d13
Tried
for i=1:nrow
fprintf(fid,'%10i, %10i, %10i, %10,\n',D(i,1),D(i,2),D(i,3),D(i,4))
end
it gives me the zeros though and I don't want them.

 Réponse acceptée

Cedric
Cedric le 15 Juil 2013
Modifié(e) : Cedric le 15 Juil 2013
The following might help (not tested):
nCol = 4 ;
Dt = D.' ;
nDt = numel(Dt) ;
for ii = 1 : nDt
if Dt(ii) ~= 0, fprintf(fid, '%10i', Dt(ii)) ; end
if ii < nDt, fprintf(fid, ',') ; end
if ~mod(ii, nCol), fprintf(fid, '\n') ; end
end
The initial transpose allows us to index Dt linearly for reading D along rows.

Plus de réponses (1)

Mark
Mark le 15 Juil 2013

0 votes

Perfect! I modified it a bit but thanks!

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by