Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
Smithy
Smithy am 3 Aug. 2022
Kommentiert: Smithy am 3 Aug. 2022
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 Kommentar
Walter Roberson
Walter Roberson am 3 Aug. 2022
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 3 Aug. 2022
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)"

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by