Hi, I would like create a vector using a for loop.

조회 수: 6 (최근 30일)
Prakash
Prakash 2018년 3월 1일
답변: Prakash 2018년 3월 1일
P=1e3;
i=6;
for i=1:length(i)
X(i)=P/1e3
end
I expect following results
X=[1e3 1e0 1e-3 1e-6 1e-9 1e-12 1e-15]
Thank you in advance!

채택된 답변

Birdman
Birdman 2018년 3월 1일
편집: Birdman 2018년 3월 1일
P=1e3;
n=7;
for i=1:n
X(i)=P/10^3;
P=P/10^3;
end

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2018년 3월 1일
n = 7;
P = 1e3;
X = P * 1e-3.^(0:n);
  댓글 수: 2
Birdman
Birdman 2018년 3월 1일
The users wants a loop.
Stephen23
Stephen23 2018년 3월 1일
편집: Stephen23 2018년 3월 1일
+1 the simpler MATLAB way of doing this.
@Birdman: lots of users come here asking for "how to do XXX in a loop", and are surprised to learn that MATLAB has simpler, more efficient ways of doing things: they did not realize that loops were not required! It is entirely appropriate on this forum to point this out, and to show how.

댓글을 달려면 로그인하십시오.


Prakash
Prakash 2018년 3월 1일
:-) Both answers are acceptable to me. Thanks!!

카테고리

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