Create an array with a for loop that refers to n user defined itterations

1 vue (au cours des 30 derniers jours)
Hello,
I am trying to create an array of length n that is completely dependent on user prompted inputs. Here is the code that I have so far:
N = input('Enter Number of Nodes: \n'); Nnodes = 1:1:N; Npipes = 1:1:N-1;
for Nnode = 1:N
X = input('Enter x-position: \n');
Y = input('Enter elevation: \n');
end
For each iteration, I want the X and Y values to be stored as separate arrays.
Thank you!
  1 commentaire
Stephen23
Stephen23 le 21 Fév 2016
Modifié(e) : Stephen23 le 21 Fév 2016
"I want the X and Y values to be stored as separate arrays" Don't do this. Read the answers to know why this is a bad idea. Put the values in vectors.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 21 Fév 2016
Modifié(e) : Stephen23 le 25 Juin 2019
Just put the values into vectors, like this:
N = str2double(input('Enter number of nodes: ','s'));
X = NaN(1,N);
Y = NaN(1,N);
for k = 1:N
X(k) = str2double(input('Enter x-position: ','s'));
Y(k) = str2double(input('Enter elevation: ','s'));
end
Avoid creating dynamically named variables in MATLAB:

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by