Expanding a double array

3 vues (au cours des 30 derniers jours)
giovanni negro
giovanni negro le 1 Fév 2018
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!

Réponses (1)

vijaya lakshmi
vijaya lakshmi le 8 Fév 2018
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

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by