フィルターのクリア

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

67 ビュー (過去 30 日間)
Ali Salim
Ali Salim 2015 年 3 月 20 日
コメント済み: Muhammad 2022 年 10 月 17 日
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 =
*
**
***
****
*****
rohit kumar
rohit kumar 2018 年 10 月 15 日
For r=1:4 Forc=1:r fprintf('*'); end fprintf('\n) end

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

採用された回答

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
Jorge Rocha
Jorge Rocha 2017 年 6 月 5 日
But how can I make the third one? (equilateral).

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

その他の回答 (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
  1 件のコメント
Muhammad
Muhammad 2022 年 10 月 17 日
*
**
***
*****
What changes we do for this type of arrangements

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

Community Treasure Hunt

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

Start Hunting!

Translated by