Define a variable?

조회 수: 6 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by