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

61 views (last 30 days)
How do I make a pattern from 1:N, Lets say N = 5, so it turns out like this:
1
12
123
1234
12345

Accepted Answer

Guillaume
Guillaume on 27 Jun 2015
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 Comments
ali b
ali b on 27 Jun 2015
Thanks for your answer, any chance it could be done using a loop?
Guillaume
Guillaume on 27 Jun 2015
Edited: Guillaume on 27 Jun 2015
Yes, just replace the arrayfun by a loop:
c = cell(1, n);
for nn = 1:n
c{nn} = 1:nn;
end

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 28 Jun 2015
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 Comments
Image Analyst
Image Analyst on 4 Apr 2020
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 on 24 Aug 2023
Edited: DGM on 24 Aug 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

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 27 Jun 2015
n=5
out=arrayfun(@(x) str2num('1':num2str(x)),1:n)'

DGM
DGM on 24 Aug 2023
Edited: DGM on 24 Aug 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

Community Treasure Hunt

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

Start Hunting!