Define a variable?

1 vue (au cours des 30 derniers jours)
gamer
gamer le 17 Juin 2021
Commenté : SungJun Cho le 17 Juin 2021
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

Réponse acceptée

SungJun Cho
SungJun Cho le 17 Juin 2021
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 commentaires
gamer
gamer le 17 Juin 2021
thanks:)
SungJun Cho
SungJun Cho le 17 Juin 2021
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by