开发者

MATLAB formatting a matrix

I am using MATLAB and having some issues formatting the output of a matrix. Currently, the matrix looks like:

  Columns 1 through 7

     4     6     5     1     0     0     0
     7     8     4     0     1     0     0
     6     5     9     0     0     1     0
     1     0     0     0     0     0    -1
     0     1     0     0     0     0     0
     0     0     1     0     0     0     0

  Columns 8 through 9

     0     0
     0     0
     0     0
     0     0
    -1     0
     0    -1

Is there a way to get the whole matrix to sh开发者_如何学Cow up "closer" together, something like this:

 1     0     0     0     0     0
 0     1     0     0     0     0
 0     0     1     0     0     0
 0     0     0    -1     0     0
 0     0     0     0    -1     0
 0     0     0     0     0    -1


To customize the output, you could try something like:

x = randi(100, [6,9]);

for i=1:size(x,1)
    fprintf('%d\t',x(i,:));
    fprintf('\n');
end

23  44  26  23  9   49  53  37  10  
18  19  41  12  27  58  24  99  27  
23  91  60  30  81  24  49  4   34  
44  98  27  32  3   46  63  89  68  
32  44  61  43  93  97  68  92  14  
93  12  72  51  74  55  40  80  73  

Or more easily, you could use the NUM2STR function:

x = randi([-10 10], [6 9]);
num2str(x)

ans =
 2   1   7   8   9  -1  -1   9   5
 6   8  -3   9  -4   3   8   4   8
-2  -9  -7   4  -9  -7  -8  -6   7
 1   6 -10   9   6   1  -1   1  -8
 9  -3  10  -5  -9   3   3   8  -7
 8  -6   4   8   5 -10  -4   1  -7
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜