Printing the entire array row in Matlab

872 ビュー (過去 30 日間)
Teoman Selcuk
Teoman Selcuk 2021 年 11 月 14 日
コメント済み: Image Analyst 2021 年 11 月 14 日
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 日
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 日
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))

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by