Define a variable?

5 ビュー (過去 30 日間)
gamer
gamer 2021 年 6 月 17 日
コメント済み: SungJun Cho 2021 年 6 月 17 日
Hello,
I want to plot n spheres. Its working but the variable k is always red underlined because I didnt defined it before the loop. How can I define this variable?
for i = 1:n
k(i) = surf(r*x+p(i,1),r*y+p(i,2),r*z+r);
end

採用された回答

SungJun Cho
SungJun Cho 2021 年 6 月 17 日
The red underline occurs when you have not preallocated your array (or matrix).
You can just do
k = zeros(1,n);
for i = 1:n
k(i) = surf(r*x+p(i,1),r*y+p(i,2),r*z+r);
end
and this should solve your problem.
  2 件のコメント
gamer
gamer 2021 年 6 月 17 日
thanks:)
SungJun Cho
SungJun Cho 2021 年 6 月 17 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by