How to combine the result of for loop?

1 次查看(过去 30 天)
Here is a very simple line of code:
x = 3;
A = 1:x;
for i = 1:length(A)
y = repelem(A(:,i),i);
Y = y(1,:)
end
I want to generate result in the form
[1 2 2 3 3 3]
What I am doing mistake, please help me in this regard. Thanks
  3 个评论
Mirlan Karimov
Mirlan Karimov 2020-5-12
x = 3;
A = 1:x;
Y = [];
for i = 1:length(A)
y = repelem(A(:,i),i);
Y(length(Y)+1:length(Y)+1 +length(y)-1) = y(1,:)
end
Muhammad Usman
Muhammad Usman 2020-5-12
Can you please suggest me how to write?

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2020-5-12
>> A = 1:3;
>> repelem(A,1,A)
ans =
1 2 2 3 3 3

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by