I have two datasets containing 100 x and 100 y coordinates respectively. I am using for loop to create a function such that the 1st entries of both data sets are represented by p1 and the second by p2 and so on. But this only assigns the values to pn and not p1,p2,p3 and so on. What should I do?
for n=1:100
pn=[x(n),y(n)]
end

3 comentarios

Stephen23
Stephen23 el 9 de En. de 2018
Editada: Stephen23 el 9 de En. de 2018
"What should I do?"
Learn to use arrays and indexing.
PS: You do not need a loop at all:
p = [x(:),y(:)];
Busy Bee
Busy Bee el 9 de En. de 2018
thank you. I need to learn a lot. :)
Jan
Jan el 9 de En. de 2018
@Busy Bee: Then you are at the right forum to get input. :-)

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 9 de En. de 2018
Editada: Jan el 9 de En. de 2018

0 votos

Do not create a set of variables. See FAQ: How to create A1, A2, ... Hiding indices in the names of variables is a frequent programming mistake made by beginners.
Fortunately there is a much easier method: Use indices as indices. :-)
p = [x, y] % or maybe [x(:), y(:)]
If you want to access the variable you tried to call "p4", use:
p(4, :)
Read the link posted by Stephen carefully. It will help you to avoid evil pitfalls.

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 9 de En. de 2018

Comentada:

Jan
el 9 de En. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by