将单元格数组折叠到文本matlab

时间:2018-04-22 07:45:31

标签: matlab

这是我猜的一个基本问题(但我是Matlab的新手),但是给出了:

16
8037
14340
21091
27903
34082

with:

fprintf('With threshold set to %d, %d motifs found at positions %f.\n',threshold,length(motifIndexAfterThresholds), motifIndexAfterThresholds);

作为该变量的内容

我希望在matlab控制台的同一行打印该变量的内容和其他一些输出:

With threshold set to 800, 6 motifs found at positions 16.000000.
With threshold set to 8037, 14340 motifs found at positions 21091.000000.
With threshold set to 27903, 34082 motifs found at positions

但是当我这样做时,我获得了多行输出:

a = [((1538, 6323), (0.9995334, 0.9995334)), ((7694, 7862, 8399, 9158),
       np.array([0.99999015, 0.99997352, 0.99997993, 0.99996219]))]

L = [(i, y[0],y[1]) for i, x in enumerate(a, 1) for y in zip(x[0], x[1])]
df = pd.DataFrame(L, columns=['ClstId','ColInt','ColFloat'])
print (df)

    ClstId  ColInt  ColFloat
0        1    1538  0.999533
1        1    6323  0.999533
2        2    7694  0.999990
3        2    7862  0.999974
4        2    8399  0.999980
5        2    9158  0.999962

有人可以分享将此双数组折叠到我可以在Matlab控制台上显示的单行文本的方法吗?

1 个答案:

答案 0 :(得分:1)

您需要的是MATLAB中内置的num2str函数。修改您的代码如下:

strThresholds = num2str(motifIndexAfterThresholds.', '%f, '); % Transpose used here since you need to make sure that motifIndexAfterThresholds is a row vector
fprintf('With threshold set to %d, %d motifs found at positions %s.\n',threshold,length(motifIndexAfterThresholds), strThresholds);

num2str函数会将向量转换为具有指定格式的字符串。所以对于你给出的例子,

strThresholds = '16.000000,  8037.000000, 14340.000000, 21091.000000, 27903.000000, 34082.000000,'

您绝对可以编辑num2str功能中使用的格式字符串以满足您的需求。我建议使用%d,因为你的向量中有整数