what does A(ones(3,1),:)) do?
11 views (last 30 days)
Show older comments
A=[1 2 3}
B= A(ones(3,1),:)
B=[1 2 3;1 2 3; 1 2 3]
please explain the second line how it works
0 Comments
Answers (1)
Thorsten
on 18 Nov 2015
Edited: Thorsten
on 18 Nov 2015
>> A = [1 2 3]
A =
1 2 3
To select the first row of A, all columns (:)
>> A(1,:)
ans =
1 2 3
You can also select the first row twice, using [1 1]:
>> A([1 1],:)
ans =
1 2 3
1 2 3
or thrice, using [1 1 1]:
>> A([1 1 1],:)
ans =
1 2 3
1 2 3
1 2 3
And you can use ones(1,3) to create [1 1 1]:
>> A(ones(1,3),:)
ans =
1 2 3
1 2 3
1 2 3
As long as it is a vector, it can be a row or a column vector, so you can also write
A(ones(3,1),:)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!