Add column vector with for loops

1 view (last 30 days)
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

Accepted Answer

Ameer Hamza
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
Massimo Fumarola
Massimo Fumarola on 25 Mar 2020
That's what I was looking for, thank you so much
Ameer Hamza
Ameer Hamza on 25 Mar 2020
Glad to be of help.

Sign in to comment.

More Answers (0)

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!