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

时间:2022-12-04 07:35:10

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

我有一个向量x =(1,2,3)我想把它显示(打印)为(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?

有人能告诉我解决办法吗?

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

编辑:x的值和(长度)都不提前知道。

6 个解决方案

#1


22  

I prefer the following, which is cleaner:

我比较喜欢下面的,比较干净:

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

which outputs

的输出

Answer: 1 2 3

#2


6  

You can use

您可以使用

x = [1, 2, 3]
disp(sprintf('Answer: (%d, %d, %d)', x))

This results in

这将导致

Answer: (1, 2, 3)

For vectors of arbitrary size, you can use

对于任意大小的向量,可以使用

disp(strrep(['Answer: (' sprintf(' %d,', x) ')'], ',)', ')'))

An alternative way would be

另一种方法是

disp(strrep(['Answer: (' num2str(x, ' %d,') ')'], ',)', ')'))

#3


4  

Here's another approach that takes advantage of Matlab's strjoin function. With strjoin it's easy to customize the delimiter between values.

这是另一种利用Matlab的strjoin函数的方法。使用strjoin,可以很容易地自定义值之间的分隔符。

x = [1, 2, 3];
fprintf('Answer: (%s)\n', strjoin(cellstr(num2str(x(:))),', '));

This results in: Answer: (1, 2, 3)

结果是:(1,2,3)

#4


1  

You might try this way:

你可以这样尝试:

fprintf('%s: (%i,%i,%i)\r\n','Answer',1,2,3)

I hope this helps.

我希望这可以帮助。

#5


1  

Here is a more generalized solution that prints all elements of x the vector x in this format:

这是一个更一般化的解决方案,它以这种格式输出x的所有元素:

x=randperm(3);
s = repmat('%d,',1,length(x));
s(end)=[]; %Remove trailing comma

disp(sprintf(['Answer: (' s ')'], x))

#6


0  

To print a vector which possibly has complex numbers-

打印一个可能有复数的向量

fprintf('Answer: %s\n', sprintf('%d ', num2str(x)));

#1


22  

I prefer the following, which is cleaner:

我比较喜欢下面的,比较干净:

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

which outputs

的输出

Answer: 1 2 3

#2


6  

You can use

您可以使用

x = [1, 2, 3]
disp(sprintf('Answer: (%d, %d, %d)', x))

This results in

这将导致

Answer: (1, 2, 3)

For vectors of arbitrary size, you can use

对于任意大小的向量,可以使用

disp(strrep(['Answer: (' sprintf(' %d,', x) ')'], ',)', ')'))

An alternative way would be

另一种方法是

disp(strrep(['Answer: (' num2str(x, ' %d,') ')'], ',)', ')'))

#3


4  

Here's another approach that takes advantage of Matlab's strjoin function. With strjoin it's easy to customize the delimiter between values.

这是另一种利用Matlab的strjoin函数的方法。使用strjoin,可以很容易地自定义值之间的分隔符。

x = [1, 2, 3];
fprintf('Answer: (%s)\n', strjoin(cellstr(num2str(x(:))),', '));

This results in: Answer: (1, 2, 3)

结果是:(1,2,3)

#4


1  

You might try this way:

你可以这样尝试:

fprintf('%s: (%i,%i,%i)\r\n','Answer',1,2,3)

I hope this helps.

我希望这可以帮助。

#5


1  

Here is a more generalized solution that prints all elements of x the vector x in this format:

这是一个更一般化的解决方案,它以这种格式输出x的所有元素:

x=randperm(3);
s = repmat('%d,',1,length(x));
s(end)=[]; %Remove trailing comma

disp(sprintf(['Answer: (' s ')'], x))

#6


0  

To print a vector which possibly has complex numbers-

打印一个可能有复数的向量

fprintf('Answer: %s\n', sprintf('%d ', num2str(x)));