storing values from a for loop

조회 수: 6 (최근 30일)
NAVNEET NAYAN
NAVNEET NAYAN 2018년 3월 11일
댓글: Adam Danz 2020년 1월 27일
clc;
clear all;
close all;
for k=-2:0.5:2
x=k;
x(k)=x;
end
I have to store values of x from -2 to 2 at a gap of 0.5 in a row vector but when I run this code I am getting an error as:
'Subscript indices must either be real positive integers or logicals.' Please tell me why am I getting this and how can it be corrected? how can I make the row vector?

채택된 답변

Von Duesenberg
Von Duesenberg 2018년 3월 11일
편집: Adam Danz 2020년 1월 27일
Is this what you had in mind?:
myValues = -2:0.5:2;
myX = zeros(length(myValues),1);
for i = 1:length(myValues)
myX(i) = myValues(i);
end
  댓글 수: 4
NAVNEET NAYAN
NAVNEET NAYAN 2018년 3월 11일
Thanks...
Adam Danz
Adam Danz 2020년 1월 27일
(My edit of the answer was just to add indentation within the i-loop).

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

추가 답변 (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