Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
Smithy
Smithy le 3 Août 2022
Commenté : Smithy le 3 Août 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 commentaire
Walter Roberson
Walter Roberson le 3 Août 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.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 3 Août 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)"
  1 commentaire
Smithy
Smithy le 3 Août 2022
Thank you very much. It works really well.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by