Matrix and For?

1 view (last 30 days)
Ali Dogan
Ali Dogan on 20 Nov 2015
Commented: Ali Dogan on 23 Nov 2015
for i=1:n
a=input('a=');
b=input('b=');
end
i want to create a matrix whose name is 'C'. C matrix should be formed;
C=[a1
b1
a2
b2
.
.
.
an
bn
]
How can i do it?

Accepted Answer

the cyclist
the cyclist on 20 Nov 2015
n = 2;
C = zeros(2*n,1);
for i=1:n
a=input('a=');
b=input('b=');
C([2*i-1 2*i]) = [a; b];
end
C

More Answers (1)

Star Strider
Star Strider on 20 Nov 2015
Edited: Star Strider on 20 Nov 2015
One way:
n = 3;
C = zeros(1,2*n);
for i=1:n
ai=inputdlg('a=');
a(i) = str2double(ai);
bi=inputdlg('b=');
b(i) = str2double(bi);
end
C = reshape([a; b], 1, [])';
I prefer inputdlg to input.
  1 Comment
Ali Dogan
Ali Dogan on 23 Nov 2015
It works too, thank you.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices 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!