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?
EDIT:
Both the values and (length of) x
are not known in advance.
You can use
This results in
For vectors of arbitrary size, you can use
An alternative way would be
You might try this way:
I hope this helps.
To print a vector which possibly has complex numbers-
Here's another approach that takes advantage of Matlab's
strjoin
function. Withstrjoin
it's easy to customize the delimiter between values.This results in:
Answer: (1, 2, 3)
Here is a more generalized solution that prints all elements of x the vector x in this format:
I prefer the following, which is cleaner:
which outputs