Filter löschen
Filter löschen

Expanding a double array

4 Ansichten (letzte 30 Tage)
giovanni negro
giovanni negro am 1 Feb. 2018
Beantwortet: vijaya lakshmi am 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!

Antworten (1)

vijaya lakshmi
vijaya lakshmi am 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by