带有空白和重新排列的填充单元阵列 [英] Pad cell array with whitespace and rearrange

查看:81
本文介绍了带有空白和重新排列的填充单元阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维单元格数组(A = 2x3),其中包含不等长的数字矢量,其格式如下:

I have a 2D cell-array (A = 2x3) containing numerical vectors of unequal length, in this form:

1x3 1x4 1x2
1x7 1x8 1x3

*Size of A (in both dimensions) can be variable

我想用空格{' '}填充每个向量,以使其长度等于lens = max(max(cellfun('length',A)));-在这种情况下,所有向量的大小将变为1x8-然后将单元格数组重新排列为这种形式,以便可以使用cell2table(使用示例数据)转换为列式表:

I want to pad each vector with whitespace {' '} to equalise their lengths to lens = max(max(cellfun('length',A)));- in this case, all vectors will become 1x8 in size - and then subsequently rearrange the cell array into this form so that it can be converted to a columnar table using cell2table (using sample data):

4   1   2   1   3   4
8   5   8   4   7   9
10  12  11  5   []  11
[]  13  21  7   []  []
[]  15  []  11  []  []
[]  18  []  23  []  []
[]  21  []  29  []  []
[]  []  []  32  []  []

[] =空格

即列的顺序为A {1,1},A {2,1},A {1,2},A {2,2},A {1,3}和A {2,3}.

i.e. columns are in the order A{1,1}, A{2,1}, A{1,2}, A{2,2}, A{1,3} and A{2,3}.

如果A = 4x3,则重新排列后的前五列将为A {1,1},A {2,1},A {3,1},A {4,1}和A {1,2 }.

If A = 4x3, the first five columns after the rearrangement would be A{1,1}, A{2,1}, A{3,1}, A{4,1} and A{1,2}.

推荐答案

我的Matlab版本(R2013a)没有cell2table,例如 Stewie Griffin ,我不确定您使用的是哪种格式需要转换.

My version of Matlab (R2013a) does not have cell2table so like Stewie Griffin I'm not sure which exact format you need for the conversion.

我也不确定double空格的填充矢量是否是一个好主意. stringsdouble不便于混合.特别是,如果您只想使用同质类型的单元格数组列(与之相对的是每个元素为cell的列).这意味着您必须:

I am also not sure if padding vectors of double with whitespace is such a good idea. strings and double are not convenient to be mixed. Specially if in your case you just want cell array columns of homogeneous type (as opposed to column where each element would be a cell). It means you have to:

  • 首先将数字转换为字符串(例如char数组).
  • 由于该列将是一个char数组,因此它们的维数必须相同,因此您必须找到最长的字符串并使它们的长度相同.
  • 最后,您可以使用必要数量的空格
  • 填充char数组列
  • convert your numbers to string first (e.g. char array).
  • since the column will be a char array, they need to be homogeneous in dimension, so you have to find the longest string and make them all the same length.
  • Finally, you can then pad you char array column with the necessary number of whitespace

一种方法需要多次cellfun调用以探查我们所需的所有这些信息,然后才能实际进行填充/整形:

One way to do that require multiple cellfun calls to probe for all these information we need before we can actually do the padding/reshaping:

%// get the length of the longest vector
Lmax = max(max(cell2mat(cellfun( @numel , A  , 'uni',0)))) ;
%// get the maximum order of magnitude
n = max(max(cell2mat(cellfun( @(x) max(ceil(log10(x))) , A  , 'uni',0)))) 
%// prepare string format based on "n"
fmt = sprintf('%%0%dd',n) ;
%// pad columns with necessary number of whitespace
b = cellfun( @(c) [num2str(c(:),fmt) ; repmat(' ', Lmax-numel(c),n)], A ,'uni',0 ) ;
%// reshape to get final desired result
b = b(:).' 

b = 
    [8x2 char]    [8x2 char]    [8x2 char]    [8x2 char]    [8x2 char]    [8x2 char]

请注意,对str2num的调用将产生原始单元格数组(几乎减少了reshape操作),因为str2num会忽略(返回empty)空格条目.

Note that a call to str2num on that would yield your original cell array (almost, less a reshape operation), as str2num will ignore (return empty) the whitespace entries.

>> bf = cellfun( @str2num , b,'un',0 )
bf = 
    [3x1 double]    [7x1 double]    [4x1 double]    [8x1 double]    [2x1 double]    [3x1 double]


如果我要处理数字,则绝对希望使用numeric类型的填充(这也使操作稍微容易一些).这是一个用'NaN's填充的示例:


If I was dealing with numbers, I would definitely prefer padding with a numeric type (also makes the operation slightly easier). Here's an example padding with 'NaN's:

%// get the length of the longest vector
Lmax = max(max(cell2mat(cellfun( @numel , A  , 'un',0)))) ;
%// pad columns with necessary number of NaN
b = cellfun( @(c) [c(:) ; NaN(Lmax-numel(c),1)], A ,'un',0 ) ;
%// reshape to get final desired result
b = b(:).' 

b = 
    [8x1 double]    [8x1 double]    [8x1 double]    [8x1 double]    [8x1 double]    [8x1 double]


如果您不喜欢使用NaN进行操作,则可以选择一个不在数据集可能值中的数字值.例如,如果您所有的值都应为正整数,则-1特殊值的良好指示.


If you do not like operating with NaNs, you could choose a numeric value which is not among the possible values of your dataset. For example if all your values are supposed to be positive integers, -1 is a good indicator of a special value.

%// choose your NULL value indicator
nullNumber = -1 ;
b = cellfun( @(c) [c.' ; zeros(Lmax-numel(c),1)+nullNumber], A ,'un',0 ) ;
b = b(:).' 

cell2mat(b)
ans =
     4     1     2     1     3     4
     8     5     8     4     7     9
    10    12    11     5    -1    11
    -1    13    21     7    -1    -1
    -1    15    -1    11    -1    -1
    -1    18    -1    23    -1    -1
    -1    21    -1    29    -1    -1
    -1    -1    -1    32    -1    -1

注意:

如果-1是您设置的可能值,而您仍然不想使用NaN,这是我行业中广泛使用的值(完全过敏)作为所有实数的null指示符是-999.25.除非您有非常特定的应用程序,否则在正常操作过程中准确获得 值的可能性非常小,以至于大多数软件算法在遇到-999.25时都能识别出null值. (有时,如果仅处理整数,则仅使用-999.)

Note:

If -1 is a possible value for your set, and you still don't want to use NaN, a value widely used in my industry (which is totally allergic to NaN) as a null indicator for all real numbers is -999.25. Unless you have a very specific application, the probability of getting exactly this value during normal operation is so infinitesimal that it is ok for most software algorithms to recognize a null value when they come across -999.25. (sometimes they use only -999 if they deal with integers only.)

还请注意在cellfun调用中使用c(:).这样可以确保将矢量(在每个单元格中)布置为(无论其原始形状如何(因为您拥有的初始矢量实际上都在 line 中)在您的示例中).

Also note the use of c(:) in the cellfun calls. This makes sure that the vector (in each cell) will be arranged as a column (regardless of it's original shape (because your initial vectors are actually in line as you have them in your example).

这篇关于带有空白和重新排列的填充单元阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆