What does the colon operator (:) in MATLAB do?

17 ビュー (過去 30 日間)
Andrew
Andrew 2011 年 3 月 21 日
コメント済み: Prakash Gautam 2022 年 6 月 27 日
The code below is used to draw a simple octagon on the screen with the word stop in the middle.
My question is, can anyone explain what is happening in the top statement. (variable t). What does the (1:2:15)' do?
Also, for example, if I wanted to draw a circle on the screen, how would I modify this code to do so.
t = (1:2:15)'*pi/8;
x = sin(t);
y = cos(t);
fill(x,y,'r')
axis square off
text(0,0,'STOP','Color',[0 0 0],'FontSize',80,'FontWeight','bold','HorizontalAlignment','center')
title('Figure STOP')

採用された回答

Paulo Silva
Paulo Silva 2011 年 3 月 21 日
1:2:15 creates a vector like this:
[1,3,5,7,9,11,13,15]
doc colon
The ' puts the values in lines (rows) instead of columns.
[1;3;5;7;9;11;13;15]
doc transpose
first vale is 1, step is 2 and last value is 15
If you want to create the circle you can do this
t=0:0.1:2*pi;
and keep the rest of the code without any changes

その他の回答 (3 件)

the cyclist
the cyclist 2011 年 3 月 21 日
x and y are being created via a "parameterization" through t. The variable t is running from 0 to 2pi (i.e. around the circumference of the circle), but it does so in eight discrete intervals. (That's why you get eight sides.)
If you breakpoint your code after the first line, you will see that t takes equally spaced values around the circle. (The "linspace" command would have been another way to do this.) The part "1:2:15" is the vector [1 3 5 7 9 11 13 15].

Alexander
Alexander 2013 年 8 月 14 日
Yes but it's been seen as := as well, what does that do?
  2 件のコメント
Jan
Jan 2013 年 8 月 15 日
At 1st this is a new question and not an answer of the original question. At 2nd ":=" might be seen anywhere, but not in valid Matlab code. In a mathematical context a ":=" means, that the left side is defined as the right side.
Image Analyst
Image Analyst 2013 年 8 月 15 日
I'm not even sure that Alexander "Answered" the question he thought he was. His response is a total non sequiter.

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


Shray Gupta
Shray Gupta 2021 年 2 月 7 日
n = [0:1/fs:t];
what does it mean
  1 件のコメント
Prakash Gautam
Prakash Gautam 2022 年 6 月 27 日
I believe 1/fs shows the time spaced for 0 to t.

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

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by