如何将数字数组与“±”字符串连接起来?

Mic*_* L. 2 string matlab

我有两个数组,结果和错误:

Results = [ 1.0 2.2 3.5 ];
Erorrs  = [ 0.2 0.2 0.3 ];
Run Code Online (Sandbox Code Playgroud)

我需要一个新的文本数组变量(可能是单元格),它的示意图如下所示:

[Results(i),'$^\pm$', Erorrs(i)]
Run Code Online (Sandbox Code Playgroud)

(示例中有 3 行)

the*_*alk 5

使用sprintf和 unicode char ± withchar(177)

for ii = 1:numel(Erorrs)
   s{ii} = sprintf('%f %c %f', Results(ii), char(177), Erorrs(ii))
end
Run Code Online (Sandbox Code Playgroud)

我认为 Latex-Interpreter 在这里不起作用,尽管我不确定。


s =

  1×3 cell array

    {'1.000000 ± 0.200000'}    {'2.200000 ± 0.200000'}    {'3.500000 ± 0.300000'}
Run Code Online (Sandbox Code Playgroud)

或者使用fprintfwith\r\n用于控制台输出。


感谢@matlabbit 的评论,还有一个矢量化版本:

compose('%f %c %f', Results(:), char(177), Erorrs(:)) 
Run Code Online (Sandbox Code Playgroud)


Sar*_*ama 5

使用字符串数组,可以这样完成:

s = string(Results) + char(177) + string(Erorrs);
Run Code Online (Sandbox Code Playgroud)
>> s
s = 
  1×3 string array
    "1±0.2"    "2.2±0.2"    "3.5±0.3"
Run Code Online (Sandbox Code Playgroud)