Clear Filters
Clear Filters

Expanding a double array

4 views (last 30 days)
giovanni negro
giovanni negro on 1 Feb 2018
Answered: vijaya lakshmi on 8 Feb 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!

Answers (1)

vijaya lakshmi
vijaya lakshmi on 8 Feb 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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!