Build array from for loop

65 views (last 30 days)
david crowley
david crowley on 10 Sep 2021
Answered: Jan on 10 Sep 2021
I have a for loop that generates an array (newdd) that changes in length in each iteration.
How do create a new variable (array) that combines each array (newdd) generated from the loop so that the output is one long array of all newdd combined?

Accepted Answer

Jan
Jan on 10 Sep 2021
n = 100;
c = cell(1, n);
for k = 1:n
c{k} = rand(1, randi(10));
end
result = cat(2, c{:});
This avoids an iteratively growing output vector, because this would need a lot of ressources.

More Answers (0)

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!