数组内容成对显示

时间:2014-09-12 08:56:03

标签: arrays matlab parsing printf

我有一个数组,例如:A=[01 255 03 122 85 107];,我想将内容打印为

  A=
   FF 01 
   7A 03   
   6B 55

基本上是从内存中读出来的。 MatLab lib中有任何功能吗?我需要尽可能少地使用循环。

1 个答案:

答案 0 :(得分:2)

使用此 -

str2num(num2str(fliplr(reshape(A,2,[])'),'%1d'))

输出 -

ans =
    21
    43
    65
    87

如果您只想将其打印为字符,请在不使用str2num的情况下使用它,如下所示 -

num2str(fliplr(reshape(A,2,[])'),'%1d')

输出 -

ans =
21
43
65
87

零填充的一般情况 -

A=[1 2 3 4 5 6 7 8 9 3] %// Input array
N = 3; %// groupings, i.e. 2 for pairs and so on
A = [A zeros(1,N-mod(numel(A),N))]; %// pad with zeros
out = str2num(num2str(fliplr(reshape(A,N,[])'),'%1d'))

输出 -

out =
   321
   654
   987
     3

编辑十六进制数字:

Ar = A(flipud(reshape(1:numel(A),2,[])))
out1 = reshape(cellstr(dec2hex(Ar))',2,[])'
out2 = [char(out1(:,1)) repmat(' ',[numel(A)/2 1]) char(out1(:,2))]

输出 -

out1 = 
    'FF'    '01'
    '7A'    '03'
    '6B'    '55'
out2 =
FF 01
7A 03
6B 55