필터 지우기
필터 지우기

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

조회 수: 4 (최근 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)"

추가 답변 (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