say i have a nXm matrix of numbers and i need to print it with tabs between itch element how should it be done?
my matrix is
0 3 0
1 2 3
and i want to print it with nonthing where the zeros are and tabs beetween the numbers
i calld he matrix z
and tried this
fprintf('%d\t%d\t%d\n',z(1,1:end))
fprintf('%d\t%d\t%d\n',z(2,1:end))
but its not working

1 件のコメント

Walter Roberson
Walter Roberson 2013 年 11 月 18 日
To check: the output should be
[TAB]3[TAB]
1[TAB]2[TAB]3
?

サインインしてコメントする。

回答 (2 件)

Sarthak
Sarthak 2023 年 2 月 20 日

0 投票

Hi,
You can use a ‘for’ loop to do the same.
Please refer to the following code to understand the problem:
z = [0 3 0; 1 2 3];
[n, m] = size(z);
for i = 1:n
for j = 1:m
if z(i,j) == 0
fprintf('\t')
else
fprintf('%d\t', z(i,j))
end
end
fprintf('\n')
end
This will produce the following output:
3
1 2 3
Jan
Jan 2023 年 2 月 20 日

0 投票

x = [0 3 0; 1 2 3];
c = sprintfc('%g', x.');
c(x.' == 0) = {''};
fprintf('\t%s\t%s\t%s\n', c{:})
3 1 2 3
% Or:
s = compose("%g", x);
s(s == "0") = "";
fprintf('\t%s\t%s\t%s\n', s.')
3 1 2 3

カテゴリ

ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索

タグ

質問済み:

Lee
2013 年 11 月 18 日

回答済み:

Jan
2023 年 2 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by