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

7 ビュー (過去 30 日間)
Smithy
Smithy 2022 年 8 月 3 日
コメント済み: Smithy 2022 年 8 月 3 日
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)"
  1 件のコメント
Smithy
Smithy 2022 年 8 月 3 日
Thank you very much. It works really well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating 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