how to create a 1,12,123,1234 pattern in a triangle

How do I make a pattern from 1:N, Lets say N = 5, so it turns out like this:
1
12
123
1234
12345

 Réponse acceptée

Use a loop or
n = 5;
c = arrayfun(@(nn) 1:nn, (1:n)', 'UniformOutput', false);
celldisp(c)
You obviously can't store the result in a matrix since matrices are square by definition.

2 commentaires

ali b
ali b le 27 Juin 2015
Thanks for your answer, any chance it could be done using a loop?
Guillaume
Guillaume le 27 Juin 2015
Modifié(e) : Guillaume le 27 Juin 2015
Yes, just replace the arrayfun by a loop:
c = cell(1, n);
for nn = 1:n
c{nn} = 1:nn;
end

Connectez-vous pour commenter.

Plus de réponses (3)

If you want a simple "for" loop way of printing this to the command window, try this:
N = 5
for k = 1 : N
% Print this line in the command window.
fprintf('%d ', 1:k);
% Scroll down to the next line.
fprintf('\n');
end

4 commentaires

Mikaila Denise Loanzon
Mikaila Denise Loanzon le 4 Avr 2020
Modifié(e) : Image Analyst le 4 Avr 2020
I tried your code and it worked. How about if I wanted the output to look like this?
n = 4
1
12
123
1234
Thank you!
Try this:
N = 5
fmt = sprintf('%%%dd', N); % format to print right-justified with N characters width
for k = 1 : N
str = sprintf('%d', 1:k); % numbers 1 through k as a string
% Print this line in the command window.
fprintf(fmt, str2num(str));
% Scroll down to the next line.
fprintf('\n');
end
Try this:
fprintf('n = 4\n'); % Print the n=4 line.
vec = [1,12,123,1234]; % Some data...
for k = 1 : length(vec)
% Print a space followed by a field of 4 for the number.
fprintf(' %4d\n', vec(k));
end
n = 4
1
12
123
1234
DGM
DGM le 24 Août 2023
Modifié(e) : DGM le 24 Août 2023
Why not go one step further?
fprintf('n = 4\n'); % print a fake assignment echo
n = 4
vec = {' 1',' 12',' 123','1234'}; % if we're going to precalculate results ...
fprintf('%s\n',vec{:}); % homework complete!
1 12 123 1234
No wait!
result = 'n = 4\n 1\n 12\n 123\n1234\n';
fprintf(result); % that was way easier!
n = 4 1 12 123 1234

Connectez-vous pour commenter.

n=5
out=arrayfun(@(x) str2num('1':num2str(x)),1:n)'

1 commentaire

Guillaume
Guillaume le 27 Juin 2015
Modifié(e) : Guillaume le 27 Juin 2015
It's clever but this will only work for n up to 9

Connectez-vous pour commenter.

DGM
DGM le 24 Août 2023
Modifié(e) : DGM le 24 Août 2023
I know this is not the only one of these threads, but I'm just going to throw this here anyway. Since I don't really care about meeting unspecified requirements of junk homework, I choose to assume my own.
I want to support n>9; in order to facilitate that, we need to prevent ambiguity by adding spaces between numbers. I choose to assume that formatting is based on a fixed field width instead of a fixed inter-field pad width. I also want to support both right and left justification of the overall line, but within each internal field, the numbers should always be right-justified. Lastly, since I think the point of this exercise is basic string formatting, I don't want to rely on the output behavior of celldisp() or similar; likewise, I don't want a bunch of array indices or other chaff printed between each output line.
Here's what I have. I'm not great with wringing elegance out of fprintf(), but it does what I expected it to do.
% n,minpad both must be positive nonzero integers
triprint(20,1,'left')
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
triprint(20,1,'right')
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
function triprint(n,minpad,justify)
% synopsis goes here ...
% validate inputs or provide defaults ...
% calculate the internal field format expression
ifw = max(ceil(log10(abs(double(fix(1:n)))+1))) + minpad; % field width
ifmt = sprintf('%%%dd',ifw); % always right-justified
for k = 1:n
rowstr = sprintf(ifmt,1:k); % block of right-justified fixed-width number fields
switch lower(justify)
case 'right'
strpad = repmat(' ',1,ifw*n - numel(rowstr)); % block of spaces
fprintf('%s%s\n',strpad,rowstr(minpad+1:end)); % print both, stripping leading pad
case 'left'
% no point adding trailing pad blocks
fprintf('%s\n',rowstr(minpad+1:end)); % print it, stripping leading pad
otherwise
error('this is an error message')
end
end
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by