Expanding a double array

3 次查看(过去 30 天)
giovanni negro
giovanni negro 2018-2-1
Hi Everybody,
I would like to solve this problem in the smartest way.
I have for example this column vector A=[3;5;6;4;...;2] and I would like to obtain another column vector like this B=[1;2;3;1;2;3;4;5;1;2;3;4;5;6;1;2;3;4;....;1;2], so B would be given for every natural number from 1 to every single element of A; an how can I reach this result?
I thought to start like this: B=1:A, but I get only B=[1;2;3]...any idea?
Thanks!

回答(1 个)

vijaya lakshmi
vijaya lakshmi 2018-2-8
Hi Giovanni,
I understand that you want to create a column vector which contains consecutive numbers, from 1 to every single element of A.
In this command "B=1:A", only the first element of A is considered.
You can try the following code snippet, to get the expected result
A=[2;4;3;5];
B=[];
for i=1:length(A)
B=vertcat(B,(1:A(i))');
end

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by