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?

 Respuesta aceptada

Von Duesenberg
Von Duesenberg el 11 de Mzo. de 2018
Editada: Adam Danz el 27 de En. de 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 comentarios

NAVNEET NAYAN
NAVNEET NAYAN el 11 de Mzo. de 2018
Thanks...it's working...My job is done....but please tell me why that error is there and can't it be corrected..
Von Duesenberg
Von Duesenberg el 11 de Mzo. de 2018
Among other things, you asked Matlab to store a value in "slot" -2 of array x. The first "slot" in an array is 1. I'm deliberately using a somewhat loose terminology because I don't know what your background is and everyday words might be clearer.
NAVNEET NAYAN
NAVNEET NAYAN el 11 de Mzo. de 2018
Thanks...
Adam Danz
Adam Danz el 27 de En. de 2020
(My edit of the answer was just to add indentation within the i-loop).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Mzo. de 2018

Comentada:

el 27 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by