Storing values in a vector using a loop

조회 수: 7 (최근 30일)
Annie
Annie 2025년 4월 19일
답변: Steven Lord 2025년 4월 20일
This is a simple version of what i'm trying to implement in a function:
d = zeros(1,N)
for i = 1:N
d(1,i)=input('Distance = ');
end
I thought I was storing the ith value in the vector, but when it saved to my workspace it came out as a scalar!?
How do I create a vector of values using a loop? Why is it not working?
  댓글 수: 4
Matt J
Matt J 2025년 4월 20일
이동: Matt J 2025년 4월 20일
Note that there is no reason you need to do this with a loop. A vector can be entered with a single input statement, e.g.,
>> d=input("Distances = ")
Distances = [1,2,5,7]
d =
1 2 5 7
Annie
Annie 2025년 4월 20일
편집: Annie 2025년 4월 20일
Yes, I'm aware. However, this is not the only case for which I'm using this method. Anyway, I realized my error and it's fixed.
Thank you though

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

답변 (1개)

Steven Lord
Steven Lord 2025년 4월 20일
Edit: Is there a way to run a function line by line like a live script?
Yes, the Debugger.

카테고리

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