Add column vector with for loops
1 view (last 30 days)
Show older comments
Massimo Fumarola
on 25 Mar 2020
Commented: Ameer Hamza
on 25 Mar 2020
I have two (4x1) column vector: ones and zeros;
I'm trying to create a matrix where every column is something like
column1 = [ones; zeros; zeros; zeros]
column2 = [zeros; ones; zeros; zeros]
column3 = [zeros; zeros; ones; zeros]
column4 = [zeros; zeros; zeros; ones]
but using a for loops.
Thanks in advance
0 Comments
Accepted Answer
Ameer Hamza
on 25 Mar 2020
Edited: Ameer Hamza
on 25 Mar 2020
See eye() function
x = repelem(eye(4),4,1);
Result
x =
1 0 0 0
1 0 0 0
1 0 0 0
1 0 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 0 1 0
0 0 1 0
0 0 1 0
0 0 1 0
0 0 0 1
0 0 0 1
0 0 0 1
0 0 0 1
4 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!