How to write a program to draw a triangle of stars in MATLAB?

84 次查看(过去 30 天)
I have a exam and don't know how to write a program to draw a triangle of stars, for example :
*
**
***
****
*****
And also flip upside down, for example :
*
**
***
****
*****
And also in the form of isosceles triangle, for example :
*
***
*****
*******
And also in the form of a specific, for example :
*
***
*****
*******
*****
***
*
Please help me!
  3 个评论
Stephen23
Stephen23 2017-9-20
编辑:Stephen23 2017-9-20
@Mohammad Hamza: your comment made me laugh. Good work.
I am not sure why so many of the answers rely on nested loops and stacks of ifs. MATLAB code should be beautiful, not written like ugly C++. Like this:
>> char(32+10*tril(ones(5)))
ans =
*
**
***
****
*****

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2015-3-20
Hint:
string = ' '; % 5 spaces.
index1 = 2;
index2 = 4;
string(index1:index2) = '*';
fprintf('%s\n', string);
You need to figure out what index1 and index2 are for the various lines you want to print out.
  5 个评论
Kunal  Kabi
Kunal Kabi 2017-6-3
编辑:Kunal Kabi 2017-6-3
The program is little bit incorrect . The correct program is:
clc;
clear all;
close all;
n=input('Enter n=');
for i=2:n
for j=1:i-1
fprintf('*');
fprintf('');
end
fprintf('\n');
end

请先登录,再进行评论。

更多回答(2 个)

Konstantinos Sofos
Konstantinos Sofos 2015-3-20
Hi,
For the first
x = [];
for i = 1 : 5
x = strcat(x,'*');
disp(x)
end
for the second
x = [];
for i = 1 : 5
x = strcat(x,'*');
s = [blanks(5-i) x];
disp(s)
end
for the third, the pyramid
x = [];
for i = 1 : 5
x = strcat(x,'*');
s = [blanks(5-i) x];
disp([s fliplr(s)])
end
  5 个评论
Dan Po
Dan Po 2016-9-26
what is the 'x=[]' for? i tried looking this up and dont know what to search for. right now i am trying to use a for loop to do the same thing, but with symmetry.
x = [];
for i = 1 : 5
x = strcat(x,'*');
s = [blanks(5-i) x];
disp([s fliplr(s)])
end
my code is this, i want to make it shorter:
row=input('enter number: \n');
y=rem(row,2);
if y==0 ;
% fprintf(2,'ERROR: you entered an even number\n')
error('ERROR: %.2f an even number\n',row)
else y>0
for i= 1:row;
for j=1:row-i
fprintf(' ')
end
for k=1:2*i-1
fprintf('*')
end
fprintf('\n')
end
for j=row-1:-1:1
for q=1:row-j
fprintf(' ')
end
for k=2*j-1:-1:1
fprintf('*')
end
fprintf('\n')
end
end
Image Analyst
Image Analyst 2016-9-26
It's to make an x that you can append to. If you don't do that in advance, then what happens when it gets to this line:
x = strcat(x,'*');
It needs to take an already existing x and add a * to it. But if you didn't assign null to x, there is no x to add a star to. So even though x is initialized to null, it's really still there, and we can append stuff to it.

请先登录,再进行评论。


Kunal  Kabi
Kunal Kabi 2017-6-3
For printing in this order
*****
***
**
*
Use this code'
clc;
clear all;
close all;
n=input('Enter n=');
for i=n:-1:2
for j=i-1:-1:1
fprintf('*');
end
fprintf('\n');
end

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by