Easiest way to make matrix have 3 sets of values

1 view (last 30 days)
Easiest way to simplify the code below?
matrix = zeros(8,1)
for i = 1:3
matrix(i,1) = 1;
end
matrix(4,1) = 2
for i = 5:8
matrix(i,1) = 3
end
Ouput
1
1
1
2
3
3
3
3
  3 Comments
Guillaume
Guillaume on 25 Feb 2020
Indeed there are many many ways to create such a simple matrix, so more details are needed on the background behind the question.
arthurk
arthurk on 25 Feb 2020
I apologise, however the answers provided all help in my case. Thank you

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 25 Feb 2020
matrix = repelem((1:3)', [3, 2, 4])
is one way

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!