How to concatenate numbers with ( ) bracket as cell type?

1 次查看(过去 30 天)
Hello everybody,
I have a matrix 10*3 and the data type is double. and if possble, I would like to know
how to make 10*1 cell with ( ) bracket after concatenating. Is there a way to make the 10*1 cell?
input = [-5,21,-5;-5,21,-3;-3,21,-5;-5,19,-5;-3,21,-3;-5,19,-3;-3,19,-5;-3,19,-3;-5,21,0;-5,16,-5];
input = num2cell(input);
% I would like to make the 10*1 cell looks as below. It has the bracket.
(-5,21,-5)
(-5,21,-3)
(-3,21,-5)
(-5,19,-5)
(-3,21,-3)
(-5,19,-3)
(-3,19,-5;)
(3,19,-3)
(-5,21,0)
(-5,16,-5)
  1 个评论
Walter Roberson
Walter Roberson 2022-8-3
the output would have to be cell array of character vectors, or else string array. You cannot create a numeric matrix in that format.
You can display a numeric matrix in that format without a lot of trouble through.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-8-3
As Walter said already: If it contains parentheses ( and ), the elements of the cell cannot be numerical.
in = [-5,21,-5;-5,21,-3;-3,21,-5;-5,19,-5;-3,21,-3;-5,19,-3;-3,19,-5;-3,19,-3;-5,21,0;-5,16,-5];
out = cell(10, 1);
for k = 1:10
out{k} = sprintf('(%g,%g,%g)', in(k, :));
end
out
out = 10×1 cell array
{'(-5,21,-5)'} {'(-5,21,-3)'} {'(-3,21,-5)'} {'(-5,19,-5)'} {'(-3,21,-3)'} {'(-5,19,-3)'} {'(-3,19,-5)'} {'(-3,19,-3)'} {'(-5,21,0)' } {'(-5,16,-5)'}
% Or:
C = compose("(%g,%g,%g)", in)
C = 10×1 string array
"(-5,21,-5)" "(-5,21,-3)" "(-3,21,-5)" "(-5,19,-5)" "(-3,21,-3)" "(-5,19,-3)" "(-3,19,-5)" "(-3,19,-3)" "(-5,21,0)" "(-5,16,-5)"

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by