How do I make this code into a function I can't manipulate=?

1 回表示 (過去 30 日間)
Emma
Emma 2021 年 3 月 4 日
コメント済み: Rik 2021 年 3 月 9 日
How do i make this code into a function?
(This code creates a graph with randomly plotted ellipses)
rng(675382) %regenerates the same values for randn
N = 20;
for R = 1:N %for loop to repeat 10 times
t = linspace(0,2*pi); % t defined angle??? not sure
xc = randn; %defines one of axis as random
x = xc + randn*cos(t);
yc = randn;
y = yc + randn*sin(t);
plot(x,y)
patch(x,y,rand)
hold on %puts all ellipses on same graph
end
My end goal is to rotate the fucntion with a rotation matrix.
  1 件のコメント
Rik
Rik 2021 年 3 月 4 日
What exactly do you want to rotate? You can't rotate a Matlab function, and your code doesn't seem to describe a mathematical function.

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

回答 (1 件)

Monisha Nalluru
Monisha Nalluru 2021 年 3 月 9 日
From my understanding you wanted to get x,y matrix after performing the operations from a function
These can be done by adding return type to function
function [X,Y] = generatematrix(N)
X=[];
Y=[];
for R = 1:N %for loop to repeat N times
t = linspace(0,2*pi);
xc = randn;
x = xc + randn*cos(t);
yc = randn;
y = yc + randn*sin(t);
X=[X;x];
Y=[Y;y];
plot(x,y)
patch(x,y,rand)
hold on
end
end
Now you can calculate rotation matrix using X,Y
  1 件のコメント
Rik
Rik 2021 年 3 月 9 日
Why no preallocation? You know how large the resulting matrix will be, so that would improve the efficiency of your code.

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

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by