Matlab字符串的串联

学生茶

我想制作一个Matlab函数,它采用两个矩阵A和B(大小相同),并以某种方式将它们组合在一起,以提供可以在Latex-table中使用的输出。

我希望输出矩阵的第一行由矩阵A的第一行组成,在它们之间有一个&符号,并以双反斜杠结束。

第二行应是B的第一行,并在其两边加上括号,并在其之间使用&符。其余的A和B依此类推。

如果允许的话A=rand(1,2),可以通过使用[num2str(A(1)), ' & ', num2str(A(2)),' \\']等等实现。

但是我希望能够创建一个对矩阵A的任何大小都可以执行此操作的函数。我想我必须以某种方式制作单元格结构。但是如何?

迪卡卡

这可能是一种方法-

%// First off, make the "mixed" matrix of A and B
AB = zeros(size(A,1)*2,size(A,2));
AB(1:2:end) = A;
AB(2:2:end) = B;

%// Convert all numbers of AB to characters with ampersands separating them
AB_amp_backslash = num2str(AB,'%1d & ');

%// Remove the ending ampersands
AB_amp_backslash(:,end-1:end) = [];

%// Append the string ` \\` and make a cell array for the final output
ABcat_char = strcat(AB_amp_backslash,' \\');
ABcat_cell = cellstr(ABcat_char)

样品运行-

A =
   183   163   116    50
   161    77   107    91
   150   124    56    46
B =
   161   108   198     4
   198    18    14   137
     6   161   188   157
ABcat_cell = 
    '183 & 163 & 116 &  50 \\'
    '161 & 108 & 198 &   4 \\'
    '161 &  77 & 107 &  91 \\'
    '198 &  18 &  14 & 137 \\'
    '150 & 124 &  56 &  46 \\'
    '  6 & 161 & 188 & 157 \\'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章