Hey i could use a small help with the For loop

for i=1:10
x=input('X wert Point=');
y=input('Y Wert Point=');
K = [x;y];
end
so this is my code and i want to get as answer K1 = [x1;y1] , k2=[x2;y2] ..... etc
it only saves the last 2 numbers i put as K=[a;b],what do i need to change?.
Thank you in advance

 Accepted Answer

Subscript ‘K’:
for i=1:10
x=input('X wert Knote=');
y=input('Y Wert Knote=');
K(:,i) = [x;y];
end
That should do what you want.

4 Comments

Oh so i have to save them as a matrix i cant make an index i suppose.
Well thank you very much for the help.
My pleasure.
I do not understand your comment. If you want to save them, you can save them as a matrix as I did here, or you can save them as a cell array:
for i=1:10
x=input('X wert Knote=');
y=input('Y Wert Knote=');
K{i} = [x;y];
end
Here, the first element of the cell array is addressed as ‘K{1}’, the second as ‘K{2}’, and so for the rest.
Saving them as dynamically-created, numbered variables such as ‘K1’, ‘K2’ and so forth is considered very bad programming practice. That creates complicated code that is prone to crashing and difficult to debug because of its complexity. Avoid it at all costs!
Allright this is very helpfull. Thank you very much
As always, my pleasure.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!