How can I get a vector from users?

I need to get a vector from user. But user should enter the values one by one (eg: 1st value is 25 2nd value is 22). When user enter the all values (n^2 values), I want to see the vector. I tried something but it is not really work. Please, give me some info about that. ****vector seems like that, vector = [45 10 2 20].* *
n=input('num of value')
costs=zeros(1,n^2);
for i=1:n^2
vector=input('enter the %d\n th value',i)
end
disp(vector)

1 comentario

Stephen23
Stephen23 el 13 de Nov. de 2018
Editada: Stephen23 el 30 de Mzo. de 2020
"But user should enter the values one by one"
That will not be a very user-friendly interface. Why not just write a function that accepts input arguments? This allows the function to be called efficiently from other code and allows it to be tested easily.

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 11 de Nov. de 2018
Try this:
% Enter as a string with numbers separted by commas or spaces
userResponse = input('enter the costs ', 's')
% Convert any commas to spaces.
userResponse = strrep(userResponse, ',', ' ')
% Convert strings to numbers.
numbers = sscanf(userResponse, '%f')

5 comentarios

Afiye Havva Geçgel
Afiye Havva Geçgel el 12 de Nov. de 2018
Editada: Afiye Havva Geçgel el 12 de Nov. de 2018
This is not fit my code format. For example the below vector is used in my code but I want to this vector from user. Firstly user enter the num of nodes and I create a vector which includes n^2 cost value. Thats why I am trying to use for loop to say fill the vector i=1:n^2 but I can not fill one by one.
costs = [100 10 2 20 100 100 5 100 100 100 100 7 100 100 100 100 ]
Image Analyst
Image Analyst el 12 de Nov. de 2018
Why? Why do it the more complicated way? Is this homework and you were asked to do it via a for loop?
Afiye Havva Geçgel
Afiye Havva Geçgel el 12 de Nov. de 2018
Not exactly, but after i get the vector, i pick some values inside the vector.
Image Analyst
Image Analyst el 12 de Nov. de 2018
You can do that with the way I gave you. Most good MATLAB programmers would not do it the way your originally said.
Afiye Havva Geçgel
Afiye Havva Geçgel el 13 de Nov. de 2018
Thank you, I will try.

Iniciar sesión para comentar.

Ali Baran Özpolat
Ali Baran Özpolat el 29 de Mzo. de 2020
I couldn't understand your question, clearly. Do you mean to create a code that calculate the power of numbers and put in a vector? But, if you want to create your own vector, please try this;
m = input('m= ');
for i= 1:1
for j=1:m
a(i,j)= input('elements= ');
v=[a]
end
end
In this code, you can create your own vector by prompt "m" value. For example, if you type m= 5, you will have 1x5 vector. Then, you can start to prompt your own integers. If you want to create a matrix with the same method, you use this code:
n = input('n= ');
m = input('m= ');
for i= 1:n
for j=1:m
a(i,j)= input('elements= ')
end
end

Categorías

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

Etiquetas

Preguntada:

el 11 de Nov. de 2018

Editada:

el 30 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by