Easiest way to make matrix have 3 sets of values

1 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2020-2-25
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 2020-2-25
I apologise, however the answers provided all help in my case. Thank you

请先登录,再进行评论。

回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by