How to append new values from a loop to the bottom of an existing matrix?

67 views (last 30 days)
Hi everyone - I have a 1x5 array of numbers ranging from 0-1 which is populated from elsewhere in the code from a loop (1st trial might produce 0, 0.4, 0.55, 0.6, 0.4, 2nd trial might produce 0.2, 0.4, 0.4, 0.5, 0.8)
But I want to create a new matrix which stores all the values from this loop as one matrix such that the 1st trial (0, 0.4, 0.55, 0.6, 0.4) is stored on the first row and the 2nd trial (0.2, 0.4, 0.4, 0.5, 0.8) is stored on the 2nd row etc so each new trial populates the new trial values onto a new row of the matrix
The closest I've got so far is:
%Number of trials
trials = 2;
for N = 1:trials
all = [];
all(N,:) = trial_values;
end
Obvs this overwrites the pre-existing values when it goes to the next trial but places them on the correct rows, would anybody be able to help point out the bit I'm missing please so that it keeps the values from the previous trial?

Answers (2)

Dyuman Joshi
Dyuman Joshi on 13 Feb 2023
Edited: Dyuman Joshi on 13 Feb 2023
Since the data here is quite small, looping twice with 5 elements each, you can make do with pre-allocating as an empty matrix, as done below -
%Number of trials
trials = 2;
all=[];
for N = 1:trials
all = [all trial_values];
end
In the above method, the array is growing with every iteration and if the number of iterations and/or the growing length are high, the whole process can easily become slow and is inefficient.
The recommended method is to pre-allocate a zeros matrix, in case of numeric data, according to the final size of the output (Read more - Pre - allocation)
trials = 2;
%pre-allocation
%use n = numel(trial_values) for a general case
%which is 5 in this case
all = zeros(1,trials*n);
for k = 1:trials
%data entries will be 1-5, 6-10, 11-15, 16-20
%so we use the loop index accordingly
all(1,(5*(k-1)+1):5*k) = trial_values;
end

Alexander Halbleib
Alexander Halbleib on 13 Feb 2023
Hi Jack,
Your issue lies in the following line:
all = [];
That line resets the whole array to an empty array, and then your assignment adds creates an empty array of size N x length(trial_values), and inserts your latest results.
Instead, you should do something like the following:
trials = 2;
all = [];
for N = 1:trials
all(N,:) = trial_values;
end
The downside here is that it's really inefficient. Every loop, Matlab has to create a new array for "all", because it's growing. There are a couple of ways to make it much faster!
The easiest way is to initialize your "all" array to be the right size:
trials = 2;
all = NaN(trials,length(trial_values));
for N = 1:trials
all(N,:) = trial_values;
end
That way, matlab doesn't have to recreate an array and copy the relevant data over every loop!
Best of luck!

Categories

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

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!