storing values from a for loop

1 次查看(过去 30 天)
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 个评论
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