storing values from a for loop

6 Ansichten (letzte 30 Tage)
NAVNEET NAYAN
NAVNEET NAYAN am 11 Mär. 2018
Kommentiert: Adam Danz am 27 Jan. 2020
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?

Akzeptierte Antwort

Von Duesenberg
Von Duesenberg am 11 Mär. 2018
Bearbeitet: Adam Danz am 27 Jan. 2020
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 Kommentare
NAVNEET NAYAN
NAVNEET NAYAN am 11 Mär. 2018
Thanks...
Adam Danz
Adam Danz am 27 Jan. 2020
(My edit of the answer was just to add indentation within the i-loop).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by