how to create a 1,12,123,1234 pattern in a triangle
61 views (last 30 days)
Show older comments
How do I make a pattern from 1:N, Lets say N = 5, so it turns out like this:
1
12
123
1234
12345
0 Comments
Accepted Answer
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.
More Answers (3)
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
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
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
vec = {' 1',' 12',' 123','1234'}; % if we're going to precalculate results ...
fprintf('%s\n',vec{:}); % homework complete!
No wait!
result = 'n = 4\n 1\n 12\n 123\n1234\n';
fprintf(result); % that was way easier!
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')
triprint(20,1,'right')
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
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!