如何在Matlab中显示(打印)矢量?

如何在Matlab中显示(打印)矢量?

问题描述:

我有一个向量x = (1, 2, 3),我想将其显示(打印)为Answer: (1, 2, 3).

I have a vector x = (1, 2, 3) and I want to display (print) it as Answer: (1, 2, 3).

我尝试了许多方法,包括:

I have tried many approaches, including:

disp('Answer: ')
strtrim(sprintf('%f ', x))

但是我仍然无法以我需要的格式打印它.

But I still can't get it to print in format which I need.

请问有人指出我的解决方案吗?

Could someone point me to the solution, please?

值和x的长​​度都未知.

Both the values and (length of) x are not known in advance.

我更喜欢以下内容,它更干净:

I prefer the following, which is cleaner:

x = [1, 2, 3];
g=sprintf('%d ', x);
fprintf('Answer: %s\n', g)

输出

Answer: 1 2 3