Printing the entire array row in Matlab

I want to print all the rows on the same line of array a and b. How would i be able to do that?
a = [12,3,4,5];
b= [4,5,12,3];
fprintf('a: %d\nb: %d',a, b)
Output:
a: 12
b: 3a: 4
b: 5a: 4
b: 5a: 12
b: 3
Expected output
a: [12,3,4,5]
b: [4,5,12,3]

댓글 수: 2

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 11월 14일
편집: KALYAN ACHARJYA 2021년 11월 14일
a = [12,3,4,5];
b= [4,5,12,3];
fprintf(['a: ' repmat(' %1.0f ',1,numel(a)) '\n'],a);
fprintf(['b: ' repmat(' %1.0f ',1,numel(b)) '\n'],b);
..
a: 12 3 4 5
b: 4 5 12 3
Image Analyst
Image Analyst 2021년 11월 14일
@KALYAN ACHARJYA, looks fine but add the enclosing brackets that he wanted and post the code down in the Answers section.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2021년 11월 14일
편집: Jan 2021년 11월 14일

8 개 추천

a = [12,3,4,5];
b = [4,5,12,3];
fprintf('a: [%s]\n', join(string(a), ','));
a: [12,3,4,5]
fprintf('b: [%s]\n', join(string(b), ','));
b: [4,5,12,3]

댓글 수: 1

Image Analyst
Image Analyst 2021년 11월 14일
+1 vote for teaching everyone about join() and string(). 👍

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2021년 11월 14일

1 개 추천

a = [12,3,4,5];
b= [4,5,12,3];
fprintf('a: [')
fprintf('%d, ', a(1:end-1))
fprintf('%d]\n', a(end))
fprintf('b: [')
fprintf('%d, ', b(1:end-1))
fprintf('%d]\n', b(end))

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2021년 11월 14일

댓글:

2021년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by